| @@ -1,3 +1,8 @@ | |||
| #' Extract matched groups from regexp | |||
| #' | |||
| #' @param text Text to search | |||
| #' @param pattern regexp | |||
| #' @inheritParams base::regexec | |||
| #' @export | |||
| run_regex <- function( | |||
| text, | |||
| @@ -8,10 +13,18 @@ run_regex <- function( | |||
| useBytes = FALSE, | |||
| invert = FALSE | |||
| ) { | |||
| # Use regex to get matches by group, gives start index and length | |||
| m <- regexec(pattern, text, ignore.case, perl, fixed, useBytes) | |||
| x <- purrr::map(m, function(mi) list('idx' = purrr::map2(mi, attr(mi, "match.length"), ~ if(.x[1] != -1) c(.x, .x + .y - 1L)))) | |||
| # Convert to start/end index | |||
| x <- purrr::map(m, function(mi) { | |||
| list( | |||
| 'idx' = purrr::map2(mi, attr(mi, "match.length"), | |||
| ~ if(.x[1] != -1) c(.x, .x + .y - 1L))) | |||
| }) | |||
| # Store text and original regexc result with same hierarchy | |||
| y <- purrr::map(text, ~ list(text = .)) | |||
| z <- purrr::map(regmatches(text, m), ~ list(m = .)) | |||
| # Zip text, indexes and regexc match object lists | |||
| purrr::map(seq_along(x), ~ list(text = y[[.]][[1]], idx = x[[.]][[1]], m = z[[.]][[1]])) | |||
| } | |||
| @@ -4,7 +4,14 @@ | |||
| \alias{regex_gadget} | |||
| \title{regexplain gadget} | |||
| \usage{ | |||
| regex_gadget(text = NULL) | |||
| regex_gadget(text = NULL, start_page = if (is.null(text)) "Text" else | |||
| "Regex") | |||
| } | |||
| \arguments{ | |||
| \item{text}{Text to explore in gadget (editable using interface)} | |||
| \item{start_page}{Open gadget to this tab, one of `"Text"`, `"Regex"`, | |||
| `"Output"`, or `"Help"`} | |||
| } | |||
| \description{ | |||
| regexplain gadget | |||
| @@ -0,0 +1,32 @@ | |||
| % Generated by roxygen2: do not edit by hand | |||
| % Please edit documentation in R/run_regex.R | |||
| \name{run_regex} | |||
| \alias{run_regex} | |||
| \title{Extract matched groups from regexp} | |||
| \usage{ | |||
| run_regex(text, pattern, ignore.case = FALSE, perl = FALSE, fixed = FALSE, | |||
| useBytes = FALSE, invert = FALSE) | |||
| } | |||
| \arguments{ | |||
| \item{text}{Text to search} | |||
| \item{pattern}{regexp} | |||
| \item{ignore.case}{if \code{FALSE}, the pattern matching is \emph{case | |||
| sensitive} and if \code{TRUE}, case is ignored during matching.} | |||
| \item{perl}{logical. Should Perl-compatible regexps be used?} | |||
| \item{fixed}{logical. If \code{TRUE}, \code{pattern} is a string to be | |||
| matched as is. Overrides all conflicting arguments.} | |||
| \item{useBytes}{logical. If \code{TRUE} the matching is done | |||
| byte-by-byte rather than character-by-character. See | |||
| \sQuote{Details}.} | |||
| \item{invert}{logical. If \code{TRUE} return indices or values for | |||
| elements that do \emph{not} match.} | |||
| } | |||
| \description{ | |||
| Extract matched groups from regexp | |||
| } | |||