| Type: | Package |
| Title: | Friendly Regular Expressions |
| Version: | 1.2.1 |
| Description: | A friendly interface for the construction of regular expressions. |
| License: | MIT + file LICENSE |
| URL: | https://github.com/kevinushey/rex |
| BugReports: | https://github.com/kevinushey/rex/issues |
| Imports: | lazyeval |
| Suggests: | covr, dplyr, ggplot2, Hmisc, knitr, magrittr, rmarkdown,roxygen2, rvest, stringr, testthat |
| VignetteBuilder: | knitr |
| Encoding: | UTF-8 |
| RoxygenNote: | 7.1.2 |
| Collate: | 'aaa.R' 'utils.R' 'escape.R' 'capture.R' 'character_class.R''counts.R' 'lookarounds.R' 'match.R' 'or.R' 'rex-mode.R''rex.R' 'shortcuts.R' 'wildcards.R' 'zzz.R' |
| NeedsCompilation: | no |
| Packaged: | 2021-11-24 20:51:05 UTC; jhester |
| Author: | Kevin Ushey [aut, cre], Jim Hester [aut], Robert Krzyzanowski [aut] |
| Maintainer: | Kevin Ushey <kevinushey@gmail.com> |
| Repository: | CRAN |
| Date/Publication: | 2021-11-26 16:11:02 UTC |
Or
Description
The special binary function%or% can be used to specify a setof optional matches.
Usage
x %or% yor(...)Arguments
x | A string. |
y | A string. |
... |
|
See Also
Other rex:capture(),character_class(),counts,group(),lookarounds,not(),rex(),shortcuts,wildcards
Regular Expression
Description
Specify an explicit regular expression. This expression must already beescaped.
Usage
## S3 method for class 'regex'as.character(x, ...)## S3 method for class 'regex'print(x, ...)regex(x, ...)Arguments
x | Object |
... | further arguments |
Methods (by generic)
as.character: coerce regex object to a characterprint: Print regex object
See Also
as.regex to coerce to a regex object.
Coerce objects to aregex.
Description
Coerce objects to aregex.
Usage
as.regex(x, ...)## Default S3 method:as.regex(x, ...)Arguments
x | Object to coerce to |
... | further arguments passed to methods. |
Methods (by class)
default: Simply escape the Object.
Create a capture group
Description
Used to save the matched value within the group for use later in the regularexpression or to extract the values captured. Both named and unnamed groupscan later be referenced usingcapture_group.
Usage
capture(..., name = NULL)capture_group(name)Arguments
... |
|
name | of the group. Unnamed capture groups are numbers starting at 1in the order they appear in the regular expression. If two groups have thesame name, the leftmost group is the used in any reference. |
See Also
group for grouping without capturing. Perl 5 CaptureGroupshttps://perldoc.perl.org/perlre#Capture-groups
Other rex:%or%(),character_class(),counts,group(),lookarounds,not(),rex(),shortcuts,wildcards
Examples
# Match paired quotation marksre <- rex( # first quotation mark capture(quotes), # match all non-matching quotation marks zero_or_more(except(capture_group(1))), # end quotation mark (matches first) capture_group(1))#named capture - don't match apples to orangesre <- rex( capture(name = "fruit", or("apple", "orange")), "=", capture_group("fruit"))Create character classes
Description
There are multiple ways you can define a character class.
Usage
character_class(x)one_of(...)any_of(..., type = c("greedy", "lazy", "possessive"))some_of(..., type = c("greedy", "lazy", "possessive"))none_of(...)except_any_of(..., type = c("greedy", "lazy", "possessive"))except_some_of(..., type = c("greedy", "lazy", "possessive"))range(start, end)`:`(start, end)exclude_range(start, end)Arguments
x | text to include in the character class (must be escaped manually) |
... |
|
type | the type of match to perform. There are three match types
|
start | beginning of character class |
end | end of character class |
Functions
character_class: explicitly define a character classone_of: matches one of the specified characters.any_of: matches zero or more of the specified characters.some_of: matches one or more of the specified characters.none_of: matches anything but one of the specified characters.except_any_of: matches zero or more of anything but the specified characters.except_some_of: matches one or more of anything but the specified characters.range: matches one of any of the characters in the range.:: matches one of any of the characters in the range.exclude_range: matches one of any of the characters except those in the range.
See Also
Other rex:%or%(),capture(),counts,group(),lookarounds,not(),rex(),shortcuts,wildcards
Examples
# grey = grayre <- rex("gr", one_of("a", "e"), "y")grepl(re, c("grey", "gray")) # TRUE TRUE# Match non-vowelsre <- rex(none_of("a", "e", "i", "o", "u"))# They can also be in the same stringre <- rex(none_of("aeiou"))grepl(re, c("k", "l", "e")) # TRUE TRUE FALSE# Match rangere <- rex(range("a", "e"))grepl(re, c("b", "d", "f")) # TRUE TRUE FALSE# Explicit creationre <- rex(character_class("abcd\\["))grepl(re, c("a", "d", "[", "]")) # TRUE TRUE TRUE FALSECharacter class escapes
Description
Character class escapes
Usage
character_class_escape(x)## S3 method for class 'regex'character_class_escape(x)## S3 method for class 'character_class'character_class_escape(x)## S3 method for class 'character'character_class_escape(x)## S3 method for class 'list'character_class_escape(x)## Default S3 method:character_class_escape(x)Arguments
x | Object to escape. |
Methods (by class)
regex: objects are passed through unchanged.character_class: objects are passed through unchanged.character: objects properly escaped for character classes.list: callcharacter_class_escapeon all elements of the list.default: coerce tocharacterandcharacter_class_escape.
Counts
Description
Functions to restrict a regex to a specific number
Usage
n_times(x, n, type = c("greedy", "lazy", "possessive"))between(x, low, high, type = c("greedy", "lazy", "possessive"))at_least(x, n, type = c("greedy", "lazy", "possessive"))at_most(x, n, type = c("greedy", "lazy", "possessive"))Arguments
x | A regex pattern. |
n | An integer number |
type | the type of match to perform. There are three match types
|
low | An integer number for the lower limit. |
high | An integer number for the upper limit. |
Functions
n_times:xmust occur exactlyntimes.between:xmust occur betweenlowandhightimes.at_least:xmust occur at leastntimes.at_most:xmust occur at mostntimes.
See Also
Other rex:%or%(),capture(),character_class(),group(),lookarounds,not(),rex(),shortcuts,wildcards
Escape characters for a regex
Description
Escape characters for a regex
Usage
escape(x)## S3 method for class 'regex'escape(x)## S3 method for class 'character_class'escape(x)## S3 method for class 'character'escape(x)## Default S3 method:escape(x)## S3 method for class 'list'escape(x)Arguments
x | Object to escape. |
Methods (by class)
regex: Objects are simply passed through unchanged.character_class: Objects are surrounded by braces.character: Objects are properly escaped for regular expressions.default: default escape coerces to character and escapes.list: simply call escape on all elements of the list.
Create a grouped expression
Description
This is similar tocapture except that it does not store thevalue of the group. Best used when you want to combine several partstogether and do not reference or extract the grouped value later.
Usage
group(...)Arguments
... |
|
See Also
capture for grouping with capturing. Perl 5 ExtendedPatternshttps://perldoc.perl.org/perlre#Extended-Patterns
Other rex:%or%(),capture(),character_class(),counts,lookarounds,not(),rex(),shortcuts,wildcards
Lookarounds
Description
Lookarounds
Usage
x %if_next_is% yx %if_next_isnt% yx %if_prev_is% yx %if_prev_isnt% yArguments
x | A regex pattern. |
y | A regex pattern. |
Details
These functions provide an interface to perl lookarounds.
Special binary functions are used to infer an ordering, since often youmight wish to match a word / set of characters conditional on the startand end of that word.
%if_next_is%:TRUEif x follows y%if_next_isnt%:TRUEif x does not follow y%if_prev_is%:TRUEif y comes before x%if_prev_isnt%:TRUEif y does not come before x
See Also
Perl 5 Documentationhttps://perldoc.perl.org/perlre#Extended-Patterns
Other rex:%or%(),capture(),character_class(),counts,group(),not(),rex(),shortcuts,wildcards
Examples
stopifnot(grepl(rex("crab" %if_next_is% "apple"), "crabapple", perl = TRUE))stopifnot(grepl(rex("crab" %if_prev_is% "apple"), "applecrab", perl = TRUE))stopifnot(grepl(rex(range("a", "e") %if_next_isnt% range("f", "g")), "ah", perl = TRUE))stopifnot(grepl(rex(range("a", "e") %if_next_is% range("f", "i")), "ah", perl = TRUE))Do not match
Description
Do not match
Usage
not(..., type = c("greedy", "lazy", "possessive"))Arguments
... |
|
type | the type of match to perform. There are three match types
|
See Also
Other rex:%or%(),capture(),character_class(),counts,group(),lookarounds,rex(),shortcuts,wildcards
Match function
Description
Match function
Usage
re_matches( data, pattern, global = FALSE, options = NULL, locations = FALSE, ...)Arguments
data | character vector to match against |
pattern | regular expression to use for matching |
global | use global matching |
options | regular expression options |
locations | rather than returning the values of the matched (orcaptured) string, return a |
... | options passed to regexpr or gregexpr |
Value
if no captures, returns a logical vector the same length as theinput character vector specifying if the relevant value matched or not. Ifthere are captures in the regular expression, returns adata.frame with acolumn for each capture group. Ifglobal isTRUE, returns alist ofdata.frames.
See Also
regexp Section "Perl-like Regular Expressions" for adiscussion of the supported options
Examples
string <- c("this is a", "test string")re_matches(string, rex("test")) # FALSE FALSE# named capturere_matches(string, rex(capture(alphas, name = "first_word"), space, capture(alphas, name = "second_word")))# first_word second_word# 1 this is# 2 test string# capture returns NA when it fails to matchre_matches(string, rex(capture("test")))# 1# 1 test# 2 <NA>Substitute regular expressions in a string with another string.
Description
Substitute regular expressions in a string with another string.
Usage
re_substitutes(data, pattern, replacement, global = FALSE, options = NULL, ...)Arguments
data | character vector to substitute |
pattern | regular expression to match |
replacement | replacement text to use |
global | substitute all occurrences |
options | option flags |
... | options passed to sub or gsub |
See Also
regexp Section "Perl-like Regular Expressions" for adiscussion of the supported options
Examples
string <- c("this is a Test", "string")re_substitutes(string, "test", "not a test", options = "insensitive")re_substitutes(string, "i", "x", global = TRUE)re_substitutes(string, "(test)", "not a \\1", options = "insensitive")Register the Rex shortcuts
Description
If you are using rex in another package you need to call this function toregister all of the rex shortcuts so that spurious NOTEs about globalvariables being generated during R CMD check.
Usage
register_shortcuts(pkg_name)Arguments
pkg_name | the package to register the shortcuts in |
Generate a regular expression.
Description
Generate a regular expression.
Usage
rex(..., env = parent.frame())Arguments
... |
|
env | environment to evaluate the rex expression in. |
See Also
Other rex:%or%(),capture(),character_class(),counts,group(),lookarounds,not(),shortcuts,wildcards
Togglesrex mode.
Description
While within rex mode, functions used within therex functionare attached, so one can get e.g. auto-completion within editors.
Usage
rex_mode()Shortcuts
Description
Commonly used character classes and regular expressions. These shortcutsare substituted insiderex calls.
Usage
shortcutsFormat
An object of classshortcut of length 116.
Details
names(shortcuts) will give you the full list of available shortcuts.
See Also
Other rex:%or%(),capture(),character_class(),counts,group(),lookarounds,not(),rex(),wildcards
Single shortcuts
Description
Each of these shortcuts has both a plural (-s) and inverse (non_) form.
Usage
single_shortcutsFormat
An object of classshortcut of length 18.
Wildcards
Description
Wildcards
Usage
zero_or_more(..., type = c("greedy", "lazy", "possessive"))one_or_more(..., type = c("greedy", "lazy", "possessive"))maybe(..., type = c("greedy", "lazy", "possessive"))Arguments
... |
|
type | the type of match to perform. There are three match types
|
Functions
zero_or_more: match...zero or more times.one_or_more: match...one or more times.maybe: match...zero or one times.
See Also
Other rex:%or%(),capture(),character_class(),counts,group(),lookarounds,not(),rex(),shortcuts