match_group {regreplaceR} | R Documentation |
This function extracts the value of a specific named group from a string using the provided regex pattern.
match_group(pattern, s, group_name, 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 extract from the match. |
ignore.case |
Logical. If TRUE, the pattern matching is case-insensitive. Default is FALSE. |
The extracted value of the specified named group, or NULL if the group is not found.
pattern <- "(?P<name>\\w+) is (?P<age>\\d+)"
s <- "Jane is 25"
match_group(pattern, s, "name")
# [1] "Jane"