re_sub {re} | R Documentation |
Substitute occurrences of a pattern in a string
Description
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
.
Usage
re_sub(pattern, replacement, string, ...)
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
|
Value
A character vector of the same length as string
, with all occurrences of the pattern replaced by
replacement
in each element.
See Also
Examples
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