re_sub {re} | R Documentation |
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
.
re_sub(pattern, replacement, string, ...)
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
|
A character vector of the same length as string
, with all occurrences of the pattern replaced by
replacement
in each element.
pattern <- re_compile("\\d+")
re_sub(pattern, "number", "Replace 123 with text.") # Replaces "123" with "number"
re_sub("\\s+", "-", "Split and join") # Replaces spaces with hyphens