replace_group {regreplaceR} | R Documentation |
Replace a Specific Named Group in a String
Description
This function replaces the value of a specific named group in a string using the provided regex pattern.
Usage
replace_group(pattern, s, group_name, replacer, ignore.case = FALSE)
Arguments
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. |
Value
A modified string where the matched value of the specified group has been replaced by the replacer
.
Examples
pattern <- "(?P<name>\\w+) is (?P<age>\\d+)"
s <- "Jane is 25"
replace_group(pattern, s, "name", "John")
# [1] "John is 25"
[Package regreplaceR version 0.1.0 Index]