Examples
Examples
The goal of this vignette is to provide some basic examples that
solve common regular expression tasks. The print and
grepl calls are attached at the end of each chain to show
the regular expression that is constructed and what it matches.
Contains two words
# find something that must contain two words
rx() %>%
rx_find("cat") %>%
rx_anything() %>%
rx_find("dog") %>%
print() %>%
grepl(c("cat dog", "cat", "dog"))
#> [1] "(cat)(.*)(dog)"
#> [1] TRUE FALSE FALSE
Between two words
# find something between two words
rx() %>%
rx_seek_prefix("cat") %>%
rx_something() %>%
rx_seek_suffix("dog") %>%
print() %>%
grepl(c("cat something dog", "catdog"), perl = TRUE)
#> [1] "(?<=cat)(.+)(?=dog)"
#> [1] TRUE FALSE
Starts with word
# find something that starts with word
rx() %>%
rx_start_of_line() %>%
rx_find("cat") %>%
print() %>%
grepl(c("cat", "cat dog", "dog cat"))
#> [1] "^(cat)"
#> [1] TRUE TRUE FALSE
Ends with word
# find something that ends with word
rx() %>%
rx_find("cat") %>%
rx_end_of_line() %>%
print() %>%
grepl(c("cat", "cat dog", "dog cat"))
#> [1] "(cat)$"
#> [1] TRUE FALSE TRUE
Starts and end with words
# find something that starts with and ends with words
rx() %>%
rx_start_of_line() %>%
rx_find("cat") %>%
rx_anything() %>%
rx_find("dog") %>%
rx_end_of_line() %>%
print() %>%
grepl(c("cat", "cat dog", "dog cat"))
#> [1] "^(cat)(.*)(dog)$"
#> [1] FALSE TRUE FALSE
Case insensitive
# find something regardless of case
rx() %>%
rx_find("cat") %>%
rx_with_any_case() %>%
print() %>%
grepl(c("cat", "CAT"))
#> [1] "(?i)(cat)"
#> [1] TRUE TRUE