Replace matched patterns in a string.

Usage

re2_replace(string, pattern, replacement, parallel = FALSE, grain_size = 1e+05, ...)
re2_replace_all(string, pattern, replacement, parallel = FALSE, grain_size = 1e+05, ...)

Arguments

string
a character vector
pattern
a pre-compiled regular expression or a string
replacement
replace the first match or all of the match of pattern in string with "rewrite"
parallel
use multithread
grain_size
a minimum chunk size for tuning the behavior of parallel algorithms
...
further arguments passed to re2

Value

For re2_replace, a character vector. For re2_replace_all, a character vector with the number of replacements.

Description

Replace the the first match or all matches of pattern in string with replacement.

Details

Within replacement, backslash-escaped digits (\1 to \9) can be used to insert text matching corresponding parenthesized group from the pattern. \0 in replacement refers to the entire matching text.

Vectorised over strings, patterns and replacements.

Examples

regexp = re2("b+") re2_replace_all("yabba dabba doo", regexp,"d")
[1] "yada dada doo" attr(,"count") [1] 2
re2_replace("yabba dabba doo", "b+","d")
[1] "yada dabba doo"
pattern = "^[\\s]+|[\\s]+$" re2_replace_all(c(" abc "," this is "), pattern, "")
[1] "abc" "this is" attr(,"count") [1] 2 2