Skip to main content

Match.named_groups

named_groupsdefault

Group: Metadata

Documentation

Gets a Dictionary containing the named capturing groups for the pattern, replacing the value for groups that did not participate in the match with default.

Note that it is possible for a group to "not participate in the match", for example with a disjunction. In the example below, the "(d)" group does not participate -- it neither matches nor fails. "ab((c)|(d))".find "abc" In this case, the group id for "(d)", which is 3, is a valid group id and (Regex.lookup_group 3) will return 3. named_groups will map a named group that does not participate to the default value.

Arguments

  • default: The value to return for a given name when the group at that index did not participate in the match.

Examples

Get the Dictionary of all of the named groups in this match, replacing

the value for groups that didn't participate in the match with "UNMATCHED".

     pattern = Regex.compile "(.. .. )(?<letters>.+)()??(?<empty>)??"
input = "aa ab abc a bc bcd"
match = pattern.match input
## match.named_groups.keys.sort == ["empty", "letters"]