Title: | Create Regular Expressions Easily |
---|---|
Description: | Build regular expressions using grammar and functionality inspired by <https://github.com/VerbalExpressions>. Usage of the %>% is encouraged to build expressions in a chain-like fashion. |
Authors: | Tyler Littlefield [aut, cre] |
Maintainer: | Tyler Littlefield <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.1 |
Built: | 2025-02-10 04:28:56 UTC |
Source: | https://github.com/verbalexpressions/rverbalexpressions |
Add this to the beginning of every verbal expression chain. This
simply returns an empty character vector so that the next step in the chain
can provide a value without explicitly writing value = "blah"
.
rx()
rx()
rx() # this rx() %>% rx_find("cat") %>% rx_anything() %>% rx_find("dog") # instead of rx_find(value = "cat") %>% rx_anything() %>% rx_find("dog")
rx() # this rx() %>% rx_find("cat") %>% rx_anything() %>% rx_find("dog") # instead of rx_find(value = "cat") %>% rx_anything() %>% rx_find("dog")
Matches both letters (case insensitive) and numbers (a through z and 0 through 9).
rx_alnum(.data = NULL, inverse = FALSE)
rx_alnum(.data = NULL, inverse = FALSE)
.data |
Expression to append, typically pulled from the pipe |
inverse |
Invert match behavior, defaults to |
rx_alnum() rx_alnum(inverse = TRUE) # create an expression x <- rx_alnum() # create input string <- "Apple 1!" # extract match regmatches(string, gregexpr(x, string))
rx_alnum() rx_alnum(inverse = TRUE) # create an expression x <- rx_alnum() # create input string <- "Apple 1!" # extract match regmatches(string, gregexpr(x, string))
Matches letters (case insensitive) only.
rx_alpha(.data = NULL, inverse = FALSE)
rx_alpha(.data = NULL, inverse = FALSE)
.data |
Expression to append, typically pulled from the pipe |
inverse |
Invert match behavior, defaults to |
rx_alpha() rx_alpha(inverse = TRUE) # create an expression x <- rx_alpha() # create input string <- "Apple 1!" # extract match regmatches(string, gregexpr(x, string))
rx_alpha() rx_alpha(inverse = TRUE) # create an expression x <- rx_alpha() # create input string <- "Apple 1!" # extract match regmatches(string, gregexpr(x, string))
Constructs a character class, sometimes called a character set. With this particular expression, you can tell the regex engine to match only one out of several characters. It does this by simply placing the characters you want to match between square brackets.
rx_any_of(.data = NULL, value)
rx_any_of(.data = NULL, value)
.data |
Expression to append, typically pulled from the pipe |
value |
Expression to optionally match |
Character class: https://www.regular-expressions.info/charclass.html
rx_any_of(value = "abc") # create an expression x <- rx_any_of(value = "abc") grepl(x, "c") # should be true grepl(x, "d") # should be false y <- rx() %>% rx_find("gr") %>% rx_any_of("ae") %>% rx_find("y") regmatches("gray", regexec(y, "gray"))[[1]] regmatches("grey", regexec(y, "grey"))[[1]]
rx_any_of(value = "abc") # create an expression x <- rx_any_of(value = "abc") grepl(x, "c") # should be true grepl(x, "d") # should be false y <- rx() %>% rx_find("gr") %>% rx_any_of("ae") %>% rx_find("y") regmatches("gray", regexec(y, "gray"))[[1]] regmatches("grey", regexec(y, "grey"))[[1]]
This expression will match everything except line breaks using
the dot and the star. The Dot .
is a
metacharacter and the Star *
is a quantifier. When
combined the expression is considered greedy because it will match everything
(except line breaks) 0 or more times.
rx_anything(.data = NULL, mode = "greedy")
rx_anything(.data = NULL, mode = "greedy")
.data |
Expression to append, typically pulled from the pipe |
mode |
Matching mode ( |
Dot: https://www.regular-expressions.info/dot.html
Star Quantifier: https://www.regular-expressions.info/repeat.html
Greedy and Lazy Quantifiers: https://www.regular-expressions.info/repeat.html#greedy
rx_anything() rx_anything(mode = "lazy") x <- rx() %>% rx_start_of_line() %>% rx_anything() %>% rx_end_of_line() grepl(x, "anything!") # this should be true grepl(rx_anything(), "") # this should be true grepl(rx_something(), "") # this should be false
rx_anything() rx_anything(mode = "lazy") x <- rx() %>% rx_start_of_line() %>% rx_anything() %>% rx_end_of_line() grepl(x, "anything!") # this should be true grepl(rx_anything(), "") # this should be true grepl(rx_something(), "") # this should be false
This expression will match everything except whatever characters
the user specifies in the value
parameter. It does this by adding a
caret symbol ^
at the beginning of a character set []
. Typing
a caret after the opening square bracket negates the character class. The
result is that the character class matches any character that is not in the
character class. Unlike the dot, negated character classes also match
(invisible) line break characters. If you don't want a negated character
class to match line breaks, you need to include the line break characters in
the class.
rx_anything_but(.data = NULL, value, mode = "greedy")
rx_anything_but(.data = NULL, value, mode = "greedy")
.data |
Expression to append, typically pulled from the pipe |
value |
Characters to not match |
mode |
Matching mode ( |
Character Class: https://www.regular-expressions.info/charclass.html
rx_anything_but(value = "abc")
rx_anything_but(value = "abc")
This function facilitates matching by providing negative assurances for surrounding symbols/groups of symbols. It allows for building expressions that are dependent on context of occurrence.
rx_avoid_prefix(.data = NULL, value) rx_avoid_suffix(.data = NULL, value)
rx_avoid_prefix(.data = NULL, value) rx_avoid_suffix(.data = NULL, value)
.data |
Expression to append, typically pulled from the pipe |
value |
Exact expression to match |
# matches any number of digits, but not preceded by "USD" rx() %>% rx_avoid_prefix('USD') %>% rx_digit() %>% rx_one_or_more() #matches a digit, but not followed by " dollars" rx() %>% rx_digit() %>% rx_avoid_suffix(' dollars')
# matches any number of digits, but not preceded by "USD" rx() %>% rx_avoid_prefix('USD') %>% rx_digit() %>% rx_one_or_more() #matches a digit, but not followed by " dollars" rx() %>% rx_digit() %>% rx_avoid_suffix(' dollars')
Begin a capture group.
rx_begin_capture(.data = NULL)
rx_begin_capture(.data = NULL)
.data |
Expression to append, typically pulled from the pipe |
Capture groups are used to extract data from within the regular expression match for further processing.
The function rx_digit()
looks for tabs with the following
expression: %%d
and matches single digit. Plural version matches
specified number of digits n
(equivalent to rx_digit() %>% rx_count(n)
).
rx_digit(.data = NULL, inverse = FALSE)
rx_digit(.data = NULL, inverse = FALSE)
.data |
Expression to append, typically pulled from the pipe |
inverse |
Invert match behavior, defaults to |
rx_digit() rx_digit(inverse = TRUE) # create an expression x <- rx_digit() # create input string <- "1 apple" # extract match regmatches(string, regexpr(x, string))
rx_digit() rx_digit(inverse = TRUE) # create an expression x <- rx_digit() # create input string <- "1 apple" # extract match regmatches(string, regexpr(x, string))
Expression to match instead. If both expressions exists, both
will be returned. This just adds the vertical bar |
often called an
alternator which allows the user to find this or that, or both!
rx_either_of(.data, ...)
rx_either_of(.data, ...)
.data |
Expression to append, typically pulled from the pipe |
... |
A character vector |
x <- rx() %>% rx_either_of("cat", "dog") %>% rx_space() %>% rx_find("food") string <- c("dog food", "cat food", "fish food") grep(x, string, value = TRUE)
x <- rx() %>% rx_either_of("cat", "dog") %>% rx_space() %>% rx_find("food") string <- c("dog food", "cat food", "fish food") grep(x, string, value = TRUE)
End a capture group.
rx_end_capture(.data = NULL)
rx_end_capture(.data = NULL)
.data |
Expression to append, typically pulled from the pipe |
Capture groups are used to extract data from within the regular expression match for further processing.
Control whether to match the expression only if it appears till
the end of the line. Basically, append a $
to the end of the
expression. The dollar sign is considered an anchor and matches the
position of characters. It can be used to "anchor" the regex match at a
certain position, in this case the dollar sign matches right after the last
character in the string.
rx_end_of_line(.data = NULL, enable = TRUE)
rx_end_of_line(.data = NULL, enable = TRUE)
.data |
Expression to match, typically pulled from the pipe |
enable |
Whether to enable this behavior, defaults to |
Anchors: https://www.regular-expressions.info/anchors.html
rx_end_of_line(enable = TRUE) rx_end_of_line(enable = FALSE) rx_end_of_line("abc", enable = TRUE) # create expression x <- rx() %>% rx_start_of_line(FALSE) %>% rx_find("apple") %>% rx_end_of_line() grepl(x, "apples") # should be false grepl(x, "apple") # should be true
rx_end_of_line(enable = TRUE) rx_end_of_line(enable = FALSE) rx_end_of_line("abc", enable = TRUE) # create expression x <- rx() %>% rx_start_of_line(FALSE) %>% rx_find("apple") %>% rx_end_of_line() grepl(x, "apples") # should be false grepl(x, "apple") # should be true
Identify a specific pattern exactly.
rx_find(.data = NULL, value)
rx_find(.data = NULL, value)
.data |
Expression to append, typically pulled from the pipe |
value |
Exact expression to match |
Capturing group: https://www.regular-expressions.info/brackets.html
Stack Overflow: https://stackoverflow.com/questions/3512471
rx_find(value = "apple") # create expression x <- rx_find(value = "apples") grepl(x, "apple") # should be false grepl(x, "apples") # should be true
rx_find(value = "apple") # create expression x <- rx_find(value = "apples") grepl(x, "apple") # should be false grepl(x, "apples") # should be true
This expression looks for line breaks, both Unix and Windows style by using the appropriate non printable characters.
rx_line_break(.data = NULL)
rx_line_break(.data = NULL)
.data |
Expression to append, typically pulled from the pipe |
Unix style: https://codepoints.net/U+000A
Windows style: https://codepoints.net/U+000D
Non printable character: https://www.regular-expressions.info/nonprint.html
rx_line_break() # create an expression x <- rx_line_break() # create input string <- "foo\nbar" # extract match regmatches(string, regexpr(x, string))
rx_line_break() # create an expression x <- rx_line_break() # create input string <- "foo\nbar" # extract match regmatches(string, regexpr(x, string))
Matches lower case letters only.
rx_lowercase(.data = NULL, inverse = FALSE)
rx_lowercase(.data = NULL, inverse = FALSE)
.data |
Expression to append, typically pulled from the pipe |
inverse |
Invert match behavior, defaults to |
rx_lowercase() rx_lowercase(inverse = TRUE) # create an expression x <- rx_lowercase() y <- rx_lowercase(inverse = TRUE) # create input string <- "Apple 1!" # extract match regmatches(string, gregexpr(x, string)) regmatches(string, gregexpr(y, string))
rx_lowercase() rx_lowercase(inverse = TRUE) # create an expression x <- rx_lowercase() y <- rx_lowercase(inverse = TRUE) # create input string <- "Apple 1!" # extract match regmatches(string, gregexpr(x, string)) regmatches(string, gregexpr(y, string))
This expression uses a quantifier ?
to optionally
match things. Specifically, the question mark makes the preceding token in
the regular expression optional.
rx_maybe(.data = NULL, value)
rx_maybe(.data = NULL, value)
.data |
Expression to append, typically pulled from the pipe |
value |
Expression to optionally match |
Quantifiers: https://www.regular-expressions.info/optional.html
rx_maybe(value = "abc") # create expression x <- rx() %>% rx_start_of_line() %>% rx_maybe("abc") %>% rx_end_of_line(enable = FALSE) grepl(x, "xyz") # should be true
rx_maybe(value = "abc") # create expression x <- rx() %>% rx_start_of_line() %>% rx_maybe("abc") %>% rx_end_of_line(enable = FALSE) grepl(x, "xyz") # should be true
Match the previous group any number of times.
rx_multiple(.data = NULL, value = NULL, min = NULL, max = NULL)
rx_multiple(.data = NULL, value = NULL, min = NULL, max = NULL)
.data |
Expression to append, typically pulled from the pipe |
value |
Item to match |
min |
Minimum number of times it should be present |
max |
Maximum number of times it should be present |
This function simply adds a * to the end of the expression.
rx_none_or_more(.data = NULL, mode = "greedy")
rx_none_or_more(.data = NULL, mode = "greedy")
.data |
Expression to append, typically pulled from the pipe |
mode |
Matching mode ( |
rx_none_or_more() # create an expression x <- rx() %>% rx_find("a") %>% rx_none_or_more() # create input input <- "aaa" # extract match regmatches(input, regexpr(x, input))
rx_none_or_more() # create an expression x <- rx() %>% rx_find("a") %>% rx_none_or_more() # create input input <- "aaa" # extract match regmatches(input, regexpr(x, input))
This expression uses a negative lookahead to ensure the
value given does not follow the previous verbal expression,
perl = TRUE
is required. For example, if you were to look for the
letter q but not the letter u you might translate this to,
"find the letter q everytime the letter u does not come after it".
rx_not(.data = NULL, value)
rx_not(.data = NULL, value)
.data |
Expression to append, typically pulled from the pipe |
value |
Value to ensure absence of |
Negative lookahead: https://www.regular-expressions.info/lookaround.html
rx_not(value = "FEB-28") # construct expression x <- rx() %>% rx_start_of_line() %>% rx_find('FEB-29') %>% rx_not("FEB-28") # create a string string <- c("FEB-29-2017", "FEB-28-2017") # extract matches, perl = TRUE is required for negative lookahead regmatches(string, regexpr(x, string, perl = TRUE)) # another example rx() %>% rx_find("q") %>% rx_not("u") %>% grepl(x = c("qu", "qa", "qq", "q", "q u"), perl = TRUE)
rx_not(value = "FEB-28") # construct expression x <- rx() %>% rx_start_of_line() %>% rx_find('FEB-29') %>% rx_not("FEB-28") # create a string string <- c("FEB-29-2017", "FEB-28-2017") # extract matches, perl = TRUE is required for negative lookahead regmatches(string, regexpr(x, string, perl = TRUE)) # another example rx() %>% rx_find("q") %>% rx_not("u") %>% grepl(x = c("qu", "qa", "qq", "q", "q u"), perl = TRUE)
This function simply adds a + to the end of the expression.
rx_one_or_more(.data = NULL, mode = "greedy")
rx_one_or_more(.data = NULL, mode = "greedy")
.data |
Expression to append, typically pulled from the pipe |
mode |
Matching mode ( |
rx_one_or_more() # create an expression x <- rx() %>% rx_find("a") %>% rx_one_or_more() # create input input <- "aaa" # extract match regmatches(input, regexpr(x, input))
rx_one_or_more() # create an expression x <- rx() %>% rx_find("a") %>% rx_one_or_more() # create input input <- "aaa" # extract match regmatches(input, regexpr(x, input))
Matches punctuation characters only:
! \" # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~
.
rx_punctuation(.data = NULL, inverse = FALSE)
rx_punctuation(.data = NULL, inverse = FALSE)
.data |
Expression to append, typically pulled from the pipe |
inverse |
Invert match behavior, defaults to |
rx_punctuation() rx_punctuation(inverse = TRUE) # create an expression x <- rx_punctuation() # create input string <- 'Apple 1!' # extract match regmatches(string, gregexpr(x, string)) # dont extract punctuation y <- rx_punctuation(inverse = TRUE) regmatches(string, gregexpr(y, string))
rx_punctuation() rx_punctuation(inverse = TRUE) # create an expression x <- rx_punctuation() # create input string <- 'Apple 1!' # extract match regmatches(string, gregexpr(x, string)) # dont extract punctuation y <- rx_punctuation(inverse = TRUE) regmatches(string, gregexpr(y, string))
Value parameter will be interpreted as pairs. For example,
range(c('a', 'z', '0', '9'))
will be interpreted to mean any
character within the ranges a–z (ascii x–y) or 0–9 (ascii x–y). The method
expects an even number of parameters; unpaired parameters are ignored.
rx_range(.data = NULL, value)
rx_range(.data = NULL, value)
.data |
Expression to append, typically pulled from the pipe |
value |
Range of characters. The method expects an even number of parameters; unpaired parameters are ignored. |
rx_range(value = c('1', '3')) # create an expression x <- rx_range(value = c('1', '3')) grepl(x, "2") # should be true grepl(x, "4") # should be false
rx_range(value = c('1', '3')) # create an expression x <- rx_range(value = c('1', '3')) grepl(x, "2") # should be true grepl(x, "4") # should be false
This function facilitates matching by providing assurances for surrounding symbols/groups of symbols. It allows for building expressions that are dependent on context of occurrence.
rx_seek_prefix(.data = NULL, value) rx_seek_suffix(.data = NULL, value)
rx_seek_prefix(.data = NULL, value) rx_seek_suffix(.data = NULL, value)
.data |
Expression to append, typically pulled from the pipe |
value |
Exact expression to match |
# this will match anything between square brackets rx() %>% rx_seek_prefix("[") %>% rx_anything("lazy") %>% rx_seek_suffix(']')
# this will match anything between square brackets rx() %>% rx_seek_prefix("[") %>% rx_anything("lazy") %>% rx_seek_suffix(']')
This expression is almost identical to rx_anything()
with one major exception, a +
is used instead of a *
. This
means rx_something()
expects something whereas
anything()
expects anything including... nothing!
rx_something(.data = NULL, mode = "greedy")
rx_something(.data = NULL, mode = "greedy")
.data |
Expression to append, typically pulled from the pipe |
mode |
Matching mode ( |
Metacharacters: https://www.regular-expressions.info/characters.html#special
Greedy and Lazy Quantifiers: https://www.regular-expressions.info/repeat.html#greedy
rx_something() # construct an expression x <- rx_something() grepl(x, "something!") # this should be true grepl(x, "") # this should be false grepl(rx_anything(), "") # this should be true
rx_something() # construct an expression x <- rx_something() grepl(x, "something!") # this should be true grepl(x, "") # this should be false grepl(rx_anything(), "") # this should be true
This expression is almost identical to rx_anything_but()
with one major exception, a +
is used instead of a *
. This
means rx_something_but()
expects something whereas
rx_anything_but()
expects anything including... nothing!
rx_something_but(.data = NULL, value, mode = "greedy")
rx_something_but(.data = NULL, value, mode = "greedy")
.data |
Expression to append, typically pulled from the pipe |
value |
Expression to optionally match |
mode |
Matching mode ( |
Metacharacters: https://www.regular-expressions.info/characters.html#special
Greedy and Lazy Quantifiers: https://www.regular-expressions.info/repeat.html#greedy
rx_something_but(value = "abc") # create an expression x <- rx_something_but(value = "python") grepl(x, "R") # should be true grepl(x, "py") # should be false
rx_something_but(value = "abc") # create an expression x <- rx_something_but(value = "python") grepl(x, "R") # should be true grepl(x, "py") # should be false
Matches a space character.
rx_space(.data = NULL, inverse = FALSE)
rx_space(.data = NULL, inverse = FALSE)
.data |
Expression to append, typically pulled from the pipe |
inverse |
Invert match behavior, defaults to |
# match space, default rx_space() # dont match space rx_space(inverse = TRUE) # create an expression x <- rx_space() # create input string <- "1 apple\t" # extract match regmatches(string, regexpr(x, string)) # extract no whitespace by inverting behavior y <- rx_space(inverse = TRUE) regmatches(string, gregexpr(y, string))
# match space, default rx_space() # dont match space rx_space(inverse = TRUE) # create an expression x <- rx_space() # create input string <- "1 apple\t" # extract match regmatches(string, regexpr(x, string)) # extract no whitespace by inverting behavior y <- rx_space(inverse = TRUE) regmatches(string, gregexpr(y, string))
Control whether to match the expression only if it appears from the beginning of the line.
rx_start_of_line(.data = NULL, enable = TRUE)
rx_start_of_line(.data = NULL, enable = TRUE)
.data |
Expression to append, typically pulled from the pipe |
enable |
Whether to enable this behavior, defaults to |
rx_start_of_line(enable = TRUE) rx_start_of_line(enable = FALSE) # create expression x <- rx() %>% rx_start_of_line() %>% rx_find("apple") grepl(x, "pineapple") # should be false grepl(x, "apple") # should be true
rx_start_of_line(enable = TRUE) rx_start_of_line(enable = FALSE) # create expression x <- rx() %>% rx_start_of_line() %>% rx_find("apple") grepl(x, "pineapple") # should be false grepl(x, "apple") # should be true
Match a tab character.
rx_tab(.data = NULL, inverse = FALSE)
rx_tab(.data = NULL, inverse = FALSE)
.data |
Expression to append, typically pulled from the pipe |
inverse |
Invert match behavior, defaults to |
This function is looks for tabs with the following
expression: \t
Tab character: https://codepoints.net/U+0009
rx_tab() rx_tab(inverse = TRUE) # create an expression x <- rx_tab() # create input string <- "foo\tbar" # extract match regmatches(string, regexpr(x, string))
rx_tab() rx_tab(inverse = TRUE) # create an expression x <- rx_tab() # create input string <- "foo\tbar" # extract match regmatches(string, regexpr(x, string))
Matches upper case letters only.
rx_uppercase(.data = NULL, inverse = FALSE)
rx_uppercase(.data = NULL, inverse = FALSE)
.data |
Expression to append, typically pulled from the pipe |
inverse |
Invert match behavior, defaults to |
rx_uppercase() rx_uppercase(inverse = TRUE) # create an expression x <- rx_uppercase() y <- rx_uppercase(inverse = TRUE) # create input string <- "Apple 1!" # extract match regmatches(string, gregexpr(x, string)) regmatches(string, gregexpr(y, string))
rx_uppercase() rx_uppercase(inverse = TRUE) # create an expression x <- rx_uppercase() y <- rx_uppercase(inverse = TRUE) # create input string <- "Apple 1!" # extract match regmatches(string, gregexpr(x, string)) regmatches(string, gregexpr(y, string))
Match a whitespace character.
rx_whitespace(.data = NULL, inverse = FALSE)
rx_whitespace(.data = NULL, inverse = FALSE)
.data |
Expression to append, typically pulled from the pipe |
inverse |
Invert match behavior, defaults to |
Match a whitespace character (one of space, tab, carriage return, new line, vertical tab and form feed).
carriage return: https://codepoints.net/U+000D
new line: https://codepoints.net/U+000
vertical tab: https://codepoints.net/U+000B
form feed: https://codepoints.net/U+000C
# match whitespace, default rx_whitespace() # dont match whitespace rx_whitespace(inverse = TRUE) # create an expression x <- rx_whitespace() # create input string <- "1 apple" # extract match regmatches(string, regexpr(x, string)) # extract no whitespace by inverting behavior y <- rx_whitespace(inverse = TRUE) regmatches(string, gregexpr(y, string))
# match whitespace, default rx_whitespace() # dont match whitespace rx_whitespace(inverse = TRUE) # create an expression x <- rx_whitespace() # create input string <- "1 apple" # extract match regmatches(string, regexpr(x, string)) # extract no whitespace by inverting behavior y <- rx_whitespace(inverse = TRUE) regmatches(string, gregexpr(y, string))
Control case-insensitive matching.
rx_with_any_case(.data = NULL, enable = TRUE)
rx_with_any_case(.data = NULL, enable = TRUE)
.data |
Expression to append, typically pulled from the pipe |
enable |
Whether to enable this behavior |
Equivalent to adding or removing the i modifier.
rx_with_any_case() # case insensitive x <- rx() %>% rx_find("abc") %>% rx_with_any_case() # case sensitive y <- rx() %>% rx_find("abc") %>% rx_with_any_case(enable = FALSE) grepl(x, "ABC") # should be true grepl(y, "ABC") # should be false
rx_with_any_case() # case insensitive x <- rx() %>% rx_find("abc") %>% rx_with_any_case() # case sensitive y <- rx() %>% rx_find("abc") %>% rx_with_any_case(enable = FALSE) grepl(x, "ABC") # should be true grepl(y, "ABC") # should be false
Match a word—a string of word characters (a–z, A–Z, 0–9 or _).
This function is looks for tabs with the following expression: \w+
rx_word(.data = NULL)
rx_word(.data = NULL)
.data |
Expression to append, typically pulled from the pipe |
rx_word() # create an expression x <- rx_word() # create inputs string1 <- "foo_bar" string2 <- "foo-bar" # extract matches regmatches(string1, regexpr(x, string1)) regmatches(string2, regexpr(x, string2)) # doesn't match -
rx_word() # create an expression x <- rx_word() # create inputs string1 <- "foo_bar" string2 <- "foo-bar" # extract matches regmatches(string1, regexpr(x, string1)) regmatches(string2, regexpr(x, string2)) # doesn't match -
Match a word character (a–z, A–Z, 0–9 or _).
rx_word_char(.data = NULL)
rx_word_char(.data = NULL)
.data |
Expression to append, typically pulled from the pipe |
rx_word_char() # Same as rx_word() x <- rx_word_char() %>% rx_one_or_more()
rx_word_char() # Same as rx_word() x <- rx_word_char() %>% rx_one_or_more()
Match beginning or end of a word—a string consisting of of word characters (a–z, A–Z, 0–9 or _).
rx_word_edge(.data = NULL)
rx_word_edge(.data = NULL)
.data |
Expression to append, typically pulled from the pipe |
rx_word_edge() x <- rx() %>% rx_word_edge() %>% rx_alpha() %>% rx_one_or_more() %>% rx_word_edge() # create inputs string1 <- "foobar" string2 <- "foo 23a bar" # matches 'foobar' regmatches(string1, regexpr(x, string1)) # matches 'foo' and 'bar' separately regmatches(string2, gregexpr(x, string2))
rx_word_edge() x <- rx() %>% rx_word_edge() %>% rx_alpha() %>% rx_one_or_more() %>% rx_word_edge() # create inputs string1 <- "foobar" string2 <- "foo 23a bar" # matches 'foobar' regmatches(string1, regexpr(x, string1)) # matches 'foo' and 'bar' separately regmatches(string2, gregexpr(x, string2))
Takes a string and escapes all characters considered special by
the regex engine. This is used internally when you add a string to the
value
parameter in most of the available functions. It is exported
and usable externally for users that want to escape all special characters
in their desired match. The following special characters are escaped
. | * ? + ( ) { } ^ $ \ : = [ ]
sanitize(x)
sanitize(x)
x |
String to sanitize |
sanitize("^") sanitize("^+") sanitize("^+?")
sanitize("^") sanitize("^+") sanitize("^+?")