regnames#

Return the names of the named capture groups in the most recent successful match.

With no argument (or a false one), you get the names that were actually visited during the match. With a truthy argument, you get every name declared by the pattern, whether or not it participated.

Synopsis#

use re 'regnames';
"foobar" =~ /(?<head>foo)(?<tail>bar)/;
my @used = regnames();      # names that participated in the match
my @all  = regnames(1);     # every name declared in the pattern

What you get back#

A flat list of names in pattern order. If there was no prior successful match in the current scope, or the pattern has no named captures at all, returns undef.

Examples#

"abc" =~ /(?<first>a)(?<second>b)(?<third>c)/;
my @n = regnames();             # ('first', 'second', 'third')

"abc" =~ /(?<a>x)|(?<a>b)/;     # branch reset-style duplicate name
my @n = regnames();             # ('a') — one entry per distinct name

my @n = regnames(1);            # all declared, even unmatched

Edge cases#

  • No prior successful match — returns undef.

  • Pattern has no named captures — returns undef.

  • Duplicate names (from branch-reset or multiple alternatives) appear once; use regname($name, 1) to see every value.