- Colorize regex in view_regex - Use exact=TRUE param to show all backslashes in wrap_regex - Rename wrap_span -> wrap_resulttags/v0.1.0
| @@ -1,6 +1,6 @@ | |||
| Package: regexhelp | |||
| Package: regexplain | |||
| Title: Rstudio addin to help you with your regexes (in progress) | |||
| Version: 0.0.0.9000 | |||
| Version: 0.0.1.9000 | |||
| Date: 2018-03-07 | |||
| Authors@R: person("Garrick", "Aden-Buie", email = "g.adenbuie@gmail.com", role = c("aut", "cre")) | |||
| Description: Test and view regexes. | |||
| @@ -17,5 +17,5 @@ Imports: | |||
| tidyr, | |||
| rstudioapi | |||
| RoxygenNote: 6.0.1 | |||
| URL: https://github.com/gadenbuie/regexhelp | |||
| BugReports: https://github.com/gadenbuie/regexhelp/issues | |||
| URL: https://github.com/gadenbuie/regexplain | |||
| BugReports: https://github.com/gadenbuie/regexplain/issues | |||
| @@ -16,7 +16,7 @@ run_regex <- function( | |||
| utils::modifyList(z, x, TRUE) | |||
| } | |||
| wrap_span <- function(x, escape = FALSE) { | |||
| wrap_result <- function(x, escape = FALSE) { | |||
| if (is.null(x$idx[[1]])) return(x$text) | |||
| text <- x$text | |||
| idx <- x$idx | |||
| @@ -63,6 +63,23 @@ wrap_span <- function(x, escape = FALSE) { | |||
| paste(out, collapse = '') | |||
| } | |||
| wrap_regex <- function(pattern, escape = TRUE, exact = TRUE) { | |||
| stopifnot(length(pattern) == 1) | |||
| if(escape) pattern <- escape_html(pattern) | |||
| r_open_parens <- "(?<![\\\\])\\(" | |||
| x <- strsplit(pattern, r_open_parens, perl = TRUE)[[1]] | |||
| first <- x[1] | |||
| x <- x[-1] | |||
| x <- paste0( | |||
| '<span class="g', sprintf("%02d", seq_along(x)), '">(', | |||
| x, | |||
| collapse = "" | |||
| ) | |||
| x <- gsub("(?<![\\\\])\\)", ")</span>", x, perl = TRUE) | |||
| if (exact) x <- gsub("\\\\", "\\\\\\\\", x) | |||
| paste0(first, x) | |||
| } | |||
| #' View grouped regex results | |||
| #' | |||
| #' @param text Text to search | |||
| @@ -71,22 +88,34 @@ wrap_span <- function(x, escape = FALSE) { | |||
| #' @param escape Escape HTML-related characters in `text`? | |||
| #' @param knitr Print into knitr doc? If `TRUE`, marks text as `asis_output` and | |||
| #' sets `render = FALSE` and `escape = TRUE`. | |||
| #' @param exact Should regex be displayed as entered by the user into R console | |||
| #' or source (default)? When `TRUE`, regex is displayed with the double `\\` | |||
| #' required for escaping backslashes in R. When `FALSE`, regex is displayed | |||
| #' as interpreted by the regex engine (i.e. double `\\` as a single `\`). | |||
| #' @param ... Passed to [run_regex] | |||
| #' @export | |||
| view_regex <- function(text, pattern, ..., render = TRUE, escape = render, knitr = FALSE) { | |||
| view_regex <- function( | |||
| text, | |||
| pattern, | |||
| ..., | |||
| render = TRUE, | |||
| escape = render, | |||
| knitr = FALSE, | |||
| exact = escape | |||
| ) { | |||
| if (knitr) { | |||
| render <- FALSE | |||
| escape <- TRUE | |||
| } | |||
| res <- run_regex(text, pattern, ...) | |||
| res <- purrr::map_chr(res, wrap_span, escape = escape) | |||
| res <- purrr::map_chr(res, wrap_result, escape = escape) | |||
| res <- paste("<p class='results'>", res, "</p>") | |||
| if (knitr) return(knitr::asis_output(res)) | |||
| if (!render) return(res) | |||
| head <- c( | |||
| "---", "pagetitle: View Regex", "---", | |||
| "<h5>Regex</h5>", | |||
| "<p><pre>", escape_html(pattern), "</pre></p>", | |||
| "<p><pre style = 'font-size: 1em;'>", wrap_regex(pattern, escape, exact), "</pre></p>", | |||
| "<h5>Results</h5>" | |||
| ) | |||
| res <- c(head, res) | |||
| @@ -0,0 +1,44 @@ | |||
| --- | |||
| title: "regexplain" | |||
| output: github_document | |||
| --- | |||
| ```{r setup, include=FALSE} | |||
| knitr::opts_chunk$set(echo = TRUE) | |||
| library(regexplain) | |||
| ``` | |||
| <!-- Links --> | |||
| [regexr]: https://regexr.com/ | |||
| ## WORK IN PROGRESS!! | |||
| regexplain is going to be an RStudio addin that helps you interactively build up your regex. | |||
| Inspired by [RegExr][regexr] and `stringr::str_view`. | |||
| ## Done (ish) | |||
| You can use `view_regex()` for a `stringr::str_view()` replacement that includes groups. | |||
| ```r | |||
| text <- c("breakfast=eggs;lunch=pizza", | |||
| "breakfast=bacon;lunch=spaghetti", | |||
| "no food here") | |||
| pattern <- "((\\w+)=)(\\w+).+(ch=s?p)" | |||
| view_regex(text, pattern) | |||
| ``` | |||
|  | |||
| ## Planned (ish) | |||
| 1. An Rstudio addin gadget that allows you to interactively enter the regex and see the results. | |||
| Like the above example, where the regex field is a text input. | |||
| 2. Import data from your environment, like a character vector, file, or data.frame column when opening the gadget. | |||
| 3. Help tab in the gadget, pulling from `?regex` but with some navigation. | |||
| 4. Tab to interactively explore output of varying regex-applying functions. In other words, see what `stringr::str_locate_all` or `stringr::str_match_all` or `grep` or `grepl` return when applying the regex to your text. | |||
| @@ -0,0 +1,43 @@ | |||
| regexplain | |||
| ================ | |||
| <!-- Links --> | |||
| ## WORK IN PROGRESS\!\! | |||
| regexplain is going to be an RStudio addin that helps you interactively | |||
| build up your regex. Inspired by [RegExr](https://regexr.com/) and | |||
| `stringr::str_view`. | |||
| ## Done (ish) | |||
| You can use `view_regex()` for a `stringr::str_view()` replacement that | |||
| includes groups. | |||
| ``` r | |||
| text <- c("breakfast=eggs;lunch=pizza", | |||
| "breakfast=bacon;lunch=spaghetti", | |||
| "no food here") | |||
| pattern <- "((\\w+)=)(\\w+).+(ch=s?p)" | |||
| view_regex(text, pattern) | |||
| ``` | |||
|  | |||
| ## Planned (ish) | |||
| 1. An Rstudio addin gadget that allows you to interactively enter the | |||
| regex and see the results. Like the above example, where the regex | |||
| field is a text input. | |||
| 2. Import data from your environment, like a character vector, file, or | |||
| data.frame column when opening the gadget. | |||
| 3. Help tab in the gadget, pulling from `?regex` but with some | |||
| navigation. | |||
| 4. Tab to interactively explore output of varying regex-applying | |||
| functions. In other words, see what `stringr::str_locate_all` or | |||
| `stringr::str_match_all` or `grep` or `grepl` return when applying | |||
| the regex to your text. | |||
| @@ -5,7 +5,7 @@ | |||
| \title{View grouped regex results} | |||
| \usage{ | |||
| view_regex(text, pattern, ..., render = TRUE, escape = render, | |||
| knitr = FALSE) | |||
| knitr = FALSE, exact = escape) | |||
| } | |||
| \arguments{ | |||
| \item{text}{Text to search} | |||
| @@ -20,6 +20,11 @@ view_regex(text, pattern, ..., render = TRUE, escape = render, | |||
| \item{knitr}{Print into knitr doc? If `TRUE`, marks text as `asis_output` and | |||
| sets `render = FALSE` and `escape = TRUE`.} | |||
| \item{exact}{Should regex be displayed as entered by the user into R console | |||
| or source (default)? When `TRUE`, regex is displayed with the double `\\` | |||
| required for escaping backslashes in R. When `FALSE`, regex is displayed | |||
| as interpreted by the regex engine (i.e. double `\\` as a single `\`).} | |||
| } | |||
| \description{ | |||
| View grouped regex results | |||