Learn R Programming

⚠️There's a newer version (0.1.1) of this package.Take me there.

RVerbalExpressions

The goal of RVerbalExpressions is to make it easier to construct regular expressions using grammar and functionality inspired by VerbalExpressions. Usage of %>% is encouraged to build expressions in a chain like fashion.

Installation

You can install RVerbalExpressions from GitHub with:

# install.packages("devtools")
devtools::install_github("VerbalExpressions/RVerbalExpressions")

Example

This is a basic example which shows you how to build a regular expression:

library(RVerbalExpressions)

# construct an expression
x <- rx_start_of_line() %>% 
  rx_find('http') %>% 
  rx_maybe('s') %>% 
  rx_find('://') %>% 
  rx_maybe('www.') %>% 
  rx_anything_but(' ') %>% 
  rx_end_of_line()

# print the expression
x
#> [1] "^(http)(s)?(\\://)(www\\.)?([^ ]*)$"

# test for a match
grepl(x, "https://www.google.com")
#> [1] TRUE

Other Implementations

You can see an up to date list of all ports on VerbalExpressions.github.io.

Additionally, there are two R packages that try to solve the same problem. I encourage you to check these out:

  1. rex by @kevinushey
  2. rebus by @richierocks

Contributing

If you find any issues, typos, etc., please file an issue or submit a PR. All contributions are welcome!

Copy Link

Version

Install

install.packages('RVerbalExpressions')

Monthly Downloads

163

Version

0.0.0.9000

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Last Published

March 20th, 2024

Functions in RVerbalExpressions (0.0.0.9000)

one_or_more

Match the previous stuff one or more times.
anything

Match any character(s) any (including zero) number of times.
start_of_line

Match the expression only if it appears from beginning of line.
something_but

Match any character(s) except these at least once.
not

Ensure that the parameter does not follow.
maybe

Optionally match an expression.
range

Match any character within the range defined by the parameters.
%>%

Pipe operator
or

Alternatively, match another expression.
tab

Match a tab character.
sanitize

Escape characters expected special by regex engines
something

Match any character(s) at least once.
with_any_case

Control case-insensitive matching.
word

Match a word.
whitespace

Match a whitespace character.
anything_but

Match any character(s) except these any (including zero) number of times.
begin_capture

Begin a capture group.
digit

Match a digit (0<U+2013>9).
br

Match a line break (alias of line_break()).
end_of_line

Match the expression only if it appears till the end of the line.
end_capture

End a capture group.
find

Match an expression.
line_break

Match a line break.
any_of

Match any of these characters exactly once.
rx_anything_but

Match any character(s) except these any (including zero) number of times.
rx_line_break

Match a line break.
rx_find

Match an expression.
rx

Constructs a Verbal Expression
rx_maybe

Optionally match an expression.
rx_not

Ensure that the parameter does not follow.
rx_with_any_case

Control case-insensitive matching.
rx_begin_capture

Begin a capture group.
rx_something_but

Match any character(s) except these at least once.
rx_start_of_line

Match the expression only if it appears from beginning of line.
rx_end_capture

End a capture group.
rx_word

Match a word.
rx_end_of_line

Match the expression only if it appears till the end of the line.
rx_tab

Match a tab character.
rx_whitespace

Match a whitespace character.
rx_br

Match a line break (alias of rx_line_break()).
rx_digit

Match a digit (0<U+2013>9).
rx_one_or_more

Match the previous stuff one or more times.
rx_or

Alternatively, match another expression.
rx_any_of

Match any of these characters exactly once.
rx_anything

Match any character(s) any (including zero) number of times.
rx_range

Match any character within the range defined by the parameters.
rx_something

Match any character(s) at least once.
rx_count

Match the previous stuff exact number of times.
rx_seek_prefix

Positive lookaround functions
rx_avoid_prefix

Negative lookaround functions
rx_none_or_more

Match the previous stuff zero or many times.
rx_lowercase

Match lower case letters.
rx_alnum

Match alphanumeric characters.
rx_alpha

Match alphabetic characters.
rx_uppercase

Match upper case letters.
rx_word_char

Match a word character.
rx_punctuation

Match punctuation characters.
rx_word_edge

Find beginning or end of a word.
rx_space

Match a space character.
rx_multiple

Match the previous group any number of times.
rx_repeat_previous

Repeat the previous item exactly count times.
rx_either_of

Alternatively, match either expression.