http://qs321.pair.com?node_id=714041

nobull has asked for the wisdom of the Perl Monks concerning the following question:

The special variables @- and @+ can give me the start and end offsets of regex captures by number. In 5.10 we can now have named captures but %- and %+ contain the actual strings matched not the offsets.

Is there any way to get the offsets from the capture names? Alternatively is there a way to convert the capture names into numbers that can be used to subscript the arrays?

The following code maps names to numbers if the regex contains only named captures but falls apart if there are named ones too.

sub named_capture_map { my %n; @n{re::regnames(1)} = (1 .. $#+); \%n; }

Is there a way of doing this that still works for regex with a mixture of named and unnamed captures?