🔍 An RStudio addin slash regex utility belt
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
Garrick Aden-Buie aea69c1f5c Break out gadget UI/server components and rename regexplain_gadget() 7 лет назад
R Break out gadget UI/server components and rename regexplain_gadget() 7 лет назад
data-raw Fix detection of octal characters in sanitize_text_input 8 лет назад
docs Fix #12 reduce downloaded package size 8 лет назад
inst Embed view_regex in html documents easily 7 лет назад
man Break out gadget UI/server components and rename regexplain_gadget() 7 лет назад
tests Better handling of results groups 7 лет назад
.Rbuildignore Update documentation 8 лет назад
.gitignore Initial commit 8 лет назад
DESCRIPTION Bump dev version -> 0.2.2.9000 7 лет назад
LICENSE.md Update regex_gadget and add Rstudio add-in 8 лет назад
NAMESPACE Break out gadget UI/server components and rename regexplain_gadget() 7 лет назад
NEWS.md Correct news: 0.2.2 not released yet 7 лет назад
Readme.Rmd Fix #12 reduce downloaded package size 8 лет назад
Readme.md Fix #12 reduce downloaded package size 8 лет назад
regexplain.Rproj Rename to regexplain 8 лет назад

Readme.md

RegExplain

Regular expressions are tricky. RegExplain makes it easier to see what you’re doing.

CRAN_Status_Badge

RegExplain is an RStudio addin slash utility belt for regular expressions. Interactively build your regexp, check the output of common string matching functions, consult the interactive help pages, or use the included resources to learn regular expressions. And more.

Inspired by RegExr.com and stringr::str_view().

Installation

Installation is easy with devtools

devtools::install_github("gadenbuie/regexplain")

or for hands-free installation

source("https://install-github.me/gadenbuie/regexplain")

RegExplain in Action

Overview

regexplain selection
regexplain selection

Regular Expressions Library

regexplain library
regexplain library

Try the Built-In Examples

regexplain examples
regexplain examples

RStudio Addin

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 <- "\\b(red|orange|yellow|green|blue|purple|white|brown)(?:\\s(\\w+))?"

Notice that RegExplain handled the extra backslashes needed for storing the RegEx characters \b, \s, and \w. Inside the gadget you can use regular old regular expressions as you found them in the wild (hello, Stack Overflow!).

Help and Cheat Sheet

The Help tab is full of resources, guides, and R packages and includes an easy-to-navigate reference of commonly used regular expression syntax.

regexplain help windows
regexplain help windows

Open RegExplain Cheatsheet from the RStudio Addins drop down to open the regex reference page in the Viewer pane without blocking your current R session.

Import Your Text

There are two ways to get your text into RegExplain. The first way was described above: select an object name or lines of text or code in the RStudio source pane and run RegExplain Selection. To import text from a file, use RegExplain File to you import the text you want to process with regular expressions.

When importing text, RegExplain automatically reduces the text to the unique entries and limits the number of lines.

regexplain addins
regexplain addins

Regular Expressions Library

The RegExplain gadget includes a regular expressions library in the RegEx tab. The library features common regular expressions, sourced from qdapRegex and Regex Hub, with several additional patterns.

The full library is stored as a JSON file in inst/extdata/patterns.json, feel free to contribute patterns you find useful or use regularly via pull request.

regexplain library modal

View Static Regex Results

RegExplain 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() 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

Notes

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. Pull requests are welcomed!