groups {regreplaceR} | R Documentation |
Extract Named Groups from a String Using a Regex Pattern
Description
This function extracts named groups from a given string using the specified regex pattern.
Named groups in the pattern are denoted using (?P<group_name>...)
syntax.
Usage
groups(pattern, s, 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. |
ignore.case |
Logical. If TRUE, the pattern matching is case-insensitive. Default is FALSE. |
Value
A named list where names are the named groups and values are the extracted strings. If no matches are found, an empty list is returned.
Examples
pattern <- "(?P<name>\\w+) is (?P<age>\\d+)"
s <- "Jane is 25"
groups(pattern, s)
# $name
# [1] "Jane"
# $age
# [1] "25"
[Package regreplaceR version 0.1.0 Index]