Skip to contents

Extracts substrings that occur between two delimiters.

Usage

strExtractBetween(x, left, right, greedy = FALSE)

Arguments

x

a character vector

left

a character string or regular expression marking the left boundary

right

a character string or regular expression marking the right boundary

greedy

logical; if TRUE, the match is greedy (longest match). If FALSE (default), the match is non-greedy (shortest match).

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 (.*) right

  • non-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"