🔍 An RStudio addin slash regex utility belt
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
Garrick Aden-Buie 49d6fec910 Add qdapRegex package to resources pirms 8 gadiem
R Add qdapRegex package to resources pirms 8 gadiem
data-raw Fix detection of octal characters in sanitize_text_input pirms 8 gadiem
docs Update Readme pirms 8 gadiem
inst Clean up regex template patterns for R pirms 8 gadiem
man Gadget is now RegExplain and clean up things pirms 8 gadiem
tests Fix unicode unescaping for e.g. \\U0001F575 pirms 8 gadiem
.Rbuildignore Initial commit pirms 8 gadiem
.gitignore Initial commit pirms 8 gadiem
DESCRIPTION Add rematch2 to Output tab options pirms 8 gadiem
LICENSE.md Update regex_gadget and add Rstudio add-in pirms 8 gadiem
NAMESPACE Announce package updates via modal on first gadget launch pirms 8 gadiem
NEWS.md Add Templates button to load common regex patterns pirms 8 gadiem
Readme.Rmd Gadget is now RegExplain and clean up things pirms 8 gadiem
Readme.md Gadget is now RegExplain and clean up things pirms 8 gadiem
regexplain.Rproj Rename to regexplain pirms 8 gadiem

Readme.md

RegExplain

CRAN_Status_Badge

regexplain is an RStudio addin that helps you interactively build up your regex expressions. Inspired by RegExr and stringr::str_view.

Installation

Installation is easy with devtools:

devtools::install_github("gadenbuie/regexplain")

Status

Mostly working, but there may be issues or future changes.

I would love your help testing this, feel free to send me your feedback on Twitter at @grrrck or through the issue tracker.

RStudio Addin

regexplain screencast
regexplain screencast

The main feature of this package is the RStudio Addin Regexplain Selection. Just select the text or object containing text (such as the variable name of a vector or a data.frame column) and run Regexplain Selection from the RStudio Addins dropdown.

regexplain in the Rstudio Addins dropdown

The addin will open an interface with 4 panes where you can

  • edit the text you’ve imported
  • build up a regex expression and interactively see it applied to your text
  • test the output of common string matching and replacement functions from base and stringr
  • and refer to a helpful cheatsheet
The panes of regexplain
The panes of regexplain

When you’re done, click on the Send Regex to Console to send your regex expression to… the console!

> pattern <- "(is|were|was) ([[:alpha:]]+) ([[:alpha:]]+)"

Additional Addins

There are two more addins. Regexplain File lets you import the text lines from a file containing the text you want to process with regular expressions. Regexplain Cheatsheet opens the help page in the Viewer pane without blocking your current R session.

View regex results without the interactivity

regexplain also provides the function view_regex() that you can use as a stringr::str_view() replacement. In addition to highlighting matched portions of the text, view_regex() also colorizes groups and attempts to colorize the regex expression itself as well.

text <- c("breakfast=eggs;lunch=pizza",
          "breakfast=bacon;lunch=spaghetti", 
          "no food here")
pattern <- "((\\w+)=)(\\w+).+(ch=s?p)"

view_regex(text, pattern)
Example view_regex(text, pattern).
Example view_regex(text, pattern).
t_nested <- "anestedgroupwithingroupexample"
r_nested <- "(a(nested)(group(within(group))(example)))"
view_regex(t_nested, r_nested)
Example of nested groups
Example of nested groups

Known Issues and Future Work

Regular expressions are nothing if not a collection of corner cases. Trying to pass regular expressions through Shiny and HTML inputs is a bit of a labyrinth. For now, assume any issues or oddities you experience with this addin are entirely my fault and have nothing to do with the fine packages this addin is built on. If you do find an issue, please file an issue.

Notes

  • I’ve set up this app so that most escape sequences don’t need to be escaped. For example, you can enter \w, whereas in R this would need to be stored as "\\w". The regex returned by the gadget will include the double backslash. In these cases the text input is not escaped by Shiny.

    Unicode and hex escape characters also do not need to be escaped, thanks to stringi::stri_unescape_unicode(). Here, "\u" is escaped by Shiny so I had to make sure they are unescaped. The list of escaped characters that get unescaped is "\\u|\\x|\\N|\\a|\\o", please let me know if you find any others that should be on this list.

Planned improvements

  • v0.1.6! Make better use of the opening view of Help tab to explain the gadget. Also add one or more additional tabs with an overview of regular expressions, with links to R4DS and vignette('regular-expressions', package = 'stringr'). Maybe also a Try This feature that sets the text and pattern with regex challenges (possibly borrowed from R4DS).

  • Add a global search option to the Regex tab. Groups would be lost, but I can still highlight global matches.

  • Add “flavors” menu to Regex tab. Automatically sets options to common flavors. Mainly for clarity when switching between “base”” style and “tidyverse/stringr” defaults.

  • Done in v0.1.5! Add replacement functions somewhere. Maybe to Output tab with g?sub and str_replace(_all)? as options with an additional “replacement” field that appears when those are selected. Or possibly as a “flavor” (see above) in the Regex tab.

  • I may add stringi functions to the list of available functions in the Output tab.

  • Done in v0.1.5! I would like to add the regex/function options for the functions in the Output tab, i similar to the options present in the Regex tab.