replace_group {regreplaceR} | R Documentation |
This function replaces the value of a specific named group in a string using the provided regex pattern.
replace_group(pattern, s, group_name, replacer, ignore.case = FALSE)
pattern |
A character string containing the regex pattern with named groups. |
s |
A character string where the pattern will be searched. |
group_name |
The name of the group to be replaced. |
replacer |
A string that will replace the matched value of the specified group. |
ignore.case |
Logical. If TRUE, the pattern matching is case-insensitive. Default is FALSE. |
A modified string where the matched value of the specified group has been replaced by the replacer
.
pattern <- "(?P<name>\\w+) is (?P<age>\\d+)"
s <- "Jane is 25"
replace_group(pattern, s, "name", "John")
# [1] "John is 25"