Uses regexec()
and regmatches()
internally.
Arguments
- x
character
. Character vector.NA
values are allowed.- pattern
character(1)
. Regular expression pattern. Evalutes withregexec()
internally.- fixed
logical(1)
. IfTRUE
,pattern
is a string to be matched as is. Otherwise, will match by regular expression.
See also
https://stringr.tidyverse.org/articles/from-base.html
https://bookdown.org/rdpeng/rprogdatascience/regular-expressions.html
https://stackoverflow.com/questions/19171715/
https://d-rug.github.io/blog/2015/regex.fick
Examples
## Regex match.
object <- strMatch(
x = c("a-b", "c-d", "e_f", NA),
pattern = "^(.+)-(.+)$",
fixed = FALSE
)
print(object)
#> [,1] [,2] [,3]
#> [1,] "a-b" "a" "b"
#> [2,] "c-d" "c" "d"
#> [3,] NA NA NA
#> [4,] NA NA NA
## Fixed match.
object <- strMatch(
x = c("a", "aa", "b", "bb"),
pattern = "a",
fixed = TRUE
)
print(object)
#> [,1]
#> [1,] "a"
#> [2,] "a"
#> [3,] NA
#> [4,] NA