re_sub
replaces all occurrences of a specified pattern (regular expression) in each element of a
character vector with a replacement string. If the provided pattern is not already a compiled pattern object, it
compiles it using re_compile
.
Arguments
- pattern
A regular expression pattern or a compiled pattern object.
- replacement
The replacement string.
- string
A character vector where each element is a string in which the pattern will be replaced.
- ...
Arguments passed on to
re_compile
IGNORECASE
Flag to indicate case-insensitive matching.
I
Abbreviation for IGNORECASE.
MULTILINE
Flag to indicate multi-line matching, where
^
and$
match the start and end of each line.M
Abbreviation for MULTILINE.
DOTALL
Flag to indicate that
.
(dot) should match any character including newline.S
Abbreviation for DOTALL
VERBOSE
Flag to allow a more verbose regex syntax, which can include comments and whitespace for readability.
X
Abbreviation for VERBOSE
NOFLAG
Flag to indicate that no flags should be set.
Value
A character vector of the same length as string
, with all occurrences of the pattern replaced by
replacement
in each element.
Examples
pattern <- re_compile("\\d+")
re_sub(pattern, "number", "Replace 123 with text.") # Replaces "123" with "number"
#> [1] "Replace number with text."
re_sub("\\s+", "-", "Split and join") # Replaces spaces with hyphens
#> [1] "Split-and-join"