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_compileIGNORECASEFlag to indicate case-insensitive matching.
IAbbreviation for IGNORECASE.
MULTILINEFlag to indicate multi-line matching, where
^and$match the start and end of each line.MAbbreviation for MULTILINE.
DOTALLFlag to indicate that
.(dot) should match any character including newline.SAbbreviation for DOTALL
VERBOSEFlag to allow a more verbose regex syntax, which can include comments and whitespace for readability.
XAbbreviation for VERBOSE
NOFLAGFlag 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"