str
that
match a given regex pattern
. Additionally, they extract matches
to every capture group, i.e. to all the subpatterns given
in round parentheses.stri_match_all(str, ..., regex)stri_match_first(str, ..., regex)
stri_match_last(str, ..., regex)
stri_match(str, ..., regex, mode = c("first", "all", "last"))
stri_match_all_regex(str, pattern, opts_regex = NULL)
stri_match_first_regex(str, pattern, opts_regex = NULL)
stri_match_last_regex(str, pattern, opts_regex = NULL)
"first"
(the default), "all"
, "last"
stri_opts_regex
; NULL
for default settings;stri_match_all*
,
a list of character matrices is returned. Each list element
represents the results of a separate search scenario.For stri_match_first*
and stri_match_last*
,
on the other hand, a character matrix is returned.
Here the search results are provided as separate rows.
The first matrix column gives the whole match. The second one corresponds to the first capture group, the third -- the second capture group, and so on.
str
and pattern
.If no pattern match is detected or if a capture group match is unavailable,
then NA
s are included in the resulting matrix (matrices), see Examples.
By the way,
stri_match
, stri_match_all
, stri_match_first
,
and stri_match_last
are convenience functions.
They just call stri_match_*_regex
-- they have been
provided for consistency with other string searching functions' wrappers,
cf. e.g. stri_extract
.
stri_extract_words
;
stri_extract
,
stri_extract_all
,
stri_extract_all_charclass
,
stri_extract_all_coll
,
stri_extract_all_regex
,
stri_extract_first
,
stri_extract_first_charclass
,
stri_extract_first_coll
,
stri_extract_first_regex
,
stri_extract_last
,
stri_extract_last_charclass
,
stri_extract_last_coll
,
stri_extract_last_regex
;
stringi-search
stri_match_all_regex("breakfast=eggs, lunch=pizza, dessert=icecream",
"(\\w+)=(\\w+)")
stri_match_all_regex(c("breakfast=eggs", "lunch=pizza", "no food here"),
"(\\w+)=(\\w+)")
stri_match_all_regex(c("breakfast=eggs;lunch=pizza",
"breakfast=bacon;lunch=spaghetti", "no food here"),
"(\\w+)=(\\w+)")
stri_match_first_regex(c("breakfast=eggs;lunch=pizza",
"breakfast=bacon;lunch=spaghetti", "no food here"),
"(\\w+)=(\\w+)")
stri_match_last_regex(c("breakfast=eggs;lunch=pizza",
"breakfast=bacon;lunch=spaghetti", "no food here"),
"(\\w+)=(\\w+)")
# Match all the pattern of the form XYX, including overlapping matches:
stri_match_all_regex("ACAGAGACTTTAGATAGAGAAGA", "(?=(([ACGT])[ACGT]\\2))")[[1]][,2]
# Compare the above to:
stri_extract_all_regex("ACAGAGACTTTAGATAGAGAAGA", "([ACGT])[ACGT]\\1")
Run the code above in your browser using DataLab