Extracts substrings that occur between two delimiters.
Value
A character vector containing the extracted substrings. If no match
is found, NA is returned for that element.
Details
The function uses regular expressions to extract the first substring
between left and right. Internally, it constructs a pattern
of the form:
greedy:
left (.*) rightnon-greedy:
left (.*?) right
Extraction is performed using stringi::stri_match_first_regex(),
which returns the first captured group.
See also
stringi::stri_match_first_regex(),
strExtract()
string-overview for an overview of all string utilities in pharos.
Examples
x <- c("a[123]b", "x[abc]y", "no match")
# non-greedy (default)
strExtractBetween(x, "\\[", "\\]")
#> [1] "123" "abc" NA
# greedy
strExtractBetween("a[1]b[2]c", "\\[", "\\]", greedy = TRUE)
#> [1] "1]b[2"
