knitr::opts_chunk$set(
  comment = "#>",
  tidy = FALSE,
  error = FALSE,
  fig.width = 8,
  fig.height = 8)

rematch

Match Regular Expressions with a Nicer 'API'

Linux Build Status Windows Build status CRAN RStudio mirror downloads Coverage Status

A small wrapper on 'regexpr' to extract the matches and captured groups from the match of a regular expression to a character vector.

Installation

source("https://install-github.me/MangoTheCat/rematch")

Usage

library(rematch)
dates <- c("2016-04-20", "1977-08-08", "not a date", "2016",
  "76-03-02", "2012-06-30", "2015-01-21 19:58")
isodate <- "([0-9]{4})-([0-1][0-9])-([0-3][0-9])"
re_match(text = dates, pattern = isodate)
isodaten <- "(?<year>[0-9]{4})-(?<month>[0-1][0-9])-(?<day>[0-3][0-9])"
re_match(text = dates, pattern = isodaten)
github_repos <- c("metacran/crandb", "jeroenooms/curl@v0.9.3",
                  "jimhester/covr#47", "hadley/dplyr@*release",
                  "mangothecat/remotes@550a3c7d3f9e1493a2ba",
                  "/$&@R64&3")
owner_rx <- "(?:(?<owner>[^/]+)/)?"
repo_rx <- "(?<repo>[^/@#]+)"
subdir_rx <- "(?:/(?<subdir>[^@#]*[^@#/]))?"
ref_rx <- "(?:@(?<ref>[^*].*))"
pull_rx <- "(?:#(?<pull>[0-9]+))"
release_rx <- "(?:@(?<release>[*]release))"
ref_or_pull_or_release_rx <-
  sprintf("(?:%s|%s|%s)?", ref_rx, pull_rx, release_rx)
github_rx <- sprintf("^(?:%s%s%s%s|(?<catchall>.*))$",
                     owner_rx, repo_rx, subdir_rx, ref_or_pull_or_release_rx)
out <- re_match(text = github_repos, pattern = github_rx)
out

License

MIT © Mango Solutions



MangoTheCat/rematch documentation built on May 8, 2019, 3:49 p.m.