Friendly Regular Expressions

A friendly interface for the construction of regular expressions.


Build Status codecov.io

Friendly Regular Expressions

Regular expressions are very powerful feature, however they are often difficult to interpret. Rex allows you to build complex regular expressions from human readable expressions. So instead of writing (and later trying to decipher)

r <- "^(?:(((?:[^:])+)://))?((?:(?:(?!:/).)*)+)(?:(:([[:digit:]]+)))?(?:(/.*))?$"

You can write

r <- rex(
 
  start,
 
  ## match the protocol -- may exist or may not
  maybe(capture(
      capture(except_some_of(":")),
      "://"
      )),
 
  ## match the path
  capture(one_or_more(not(":/"))),
 
  ## get the port
  maybe(capture(":", capture(numbers))),
 
  ## and the rest
  maybe(capture("/", anything)),
 
  end
 
)

While these expressions are a big longer than their corresponding regular expression, they are much more readable and maintainable.

Installation

library(devtools)
install_github("kevinushey/rex")

Usage

The vignettes have longer form usage examples.

Each rex() function call can include a number of functions and shortcuts. For a full list of the functions available please see ?rex and ?shortcuts.

Rex Mode

Rex functions are not exported because they are only useful within rex() calls, but they can be temporarily attached using rex_mode() which allows them to be auto-completed.

Using Rex in other packages

Using rex in other packages will generate spurious NOTEs from R CMD check unless you include a call to rex::register_shortcuts() with your package name somewhere in your package source. This function registers all of the rex shortcuts as valid variables fixing the NOTEs.

See Also

  • Regularity - Ruby library that partially inspired rex.
  • PCRE - Perl Compatible Regular Expressions, the engine that rex regular expressions use.
  • Perl 5 Regular Expressions - Perl regular expression documentation, which are nearly 100% compatible with PCRE.

News

Rex Version 1.1.2

  • Updating tests to work with testthat version 1.0.2.9000.

  • Add m, matches and s, substitutes aliases for re_matches and re_substitutes.

Rex Version 1.1.1

  • Vignette tweak for ggplot2 2.0.0
  • Only print startup message some of the time.
  • Move register for magrittr pipe to .onLoad()

Rex Version 1.0.1

  • Work around ggplot2 bug with windows fonts

Rex Version 1.0.0

  • Include the capture results even if locations = TRUE
  • Add : operator for character ranges
  • Remove duplicate regex functino
  • Don't re-compute missing names
  • Reduce code duplication
  • Add examples for lookarounds

Rex Version 0.2.0

Enhancements

  • Add a newline shortcut
  • add register_shortcuts to allow use of rex in external packages without spurious NOTES.

Rex Version 0.1.1

Enhancements

  • re_matches now has a "locations" argument, which returns the start and end locations of the match or capture(s).
  • Simplify regular expressions generated from 'some_of' functions.

Bug fixes

  • backslashes ("\") are now properly escaped.

Misc

  • Improve Rex mode documentation (#21 @Ironholds)
  • Improve Log parsing Vignette copy and Title (#18, #20 @Ironholds)
  • Add links to GitHub and issues page in DESCRIPTION

Rex Version 0.1.0

Initial release

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("rex")

1.1.2 by Jim Hester, 9 months ago


https://github.com/kevinushey/rex


Report a bug at https://github.com/kevinushey/rex/issues


Browse source code at https://github.com/cran/rex


Authors: Kevin Ushey [aut], Jim Hester [aut, cre], Robert Krzyzanowski [aut]


Documentation:   PDF Manual  


MIT + file LICENSE license


Imports magrittr, lazyeval

Suggests testthat, knitr, rmarkdown, dplyr, ggplot2, Hmisc, stringr, rvest, roxygen2, covr


Imported by RxODE, covr, lintr, nlmixr.

Suggested by dparser, gramEvol, mlr, mlrCPO, ore.


See at CRAN