Extracts the first substring matching a regular expression from each element of a character vector.
Arguments
- x
a character vector
- pattern
a character string containing a regular expression to match
- ...
additional arguments passed to
stringi::stri_extract_first_regex()
Value
A character vector containing the first match for each element of
x. If no match is found, NA is returned.
Details
This function is a thin wrapper around
stringi::stri_extract_first_regex() providing a simplified
interface for extracting the first match of a pattern.
See also
stringi::stri_extract_first_regex(),
strExtractBetween()
string-overview for an overview of all string utilities in pharos.
Examples
x <- c("abc123", "no digits", "456xyz")
# extract first number
strExtract(x, "\\d+")
#> [1] "123" NA "456"
# extract words
strExtract("hello world", "\\w+")
#> [1] "hello"
