| #' Extract matched groups from regexp | |||||
| #' | |||||
| #' @param text Text to search | |||||
| #' @param pattern regexp | |||||
| #' @inheritParams base::regexec | |||||
| #' @export | #' @export | ||||
| run_regex <- function( | run_regex <- function( | ||||
| text, | text, | ||||
| useBytes = FALSE, | useBytes = FALSE, | ||||
| invert = FALSE | invert = FALSE | ||||
| ) { | ) { | ||||
| # Use regex to get matches by group, gives start index and length | |||||
| m <- regexec(pattern, text, ignore.case, perl, fixed, useBytes) | 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 = .)) | y <- purrr::map(text, ~ list(text = .)) | ||||
| z <- purrr::map(regmatches(text, m), ~ list(m = .)) | 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]])) | purrr::map(seq_along(x), ~ list(text = y[[.]][[1]], idx = x[[.]][[1]], m = z[[.]][[1]])) | ||||
| } | } | ||||
| \alias{regex_gadget} | \alias{regex_gadget} | ||||
| \title{regexplain gadget} | \title{regexplain gadget} | ||||
| \usage{ | \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{ | \description{ | ||||
| regexplain gadget | regexplain gadget |
| % 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 | |||||
| } |