Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Finding match offsets of named captures in 5.10 regex

by nobull (Friar)
on Sep 27, 2008 at 17:21 UTC ( #714041=perlquestion: print w/ replies, xml ) Need Help??
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?

Comment on Finding match offsets of named captures in 5.10 regex
Select or Download Code
Re: Finding match offsets of named captures in 5.10 regex
by JavaFan (Canon) on Sep 27, 2008 at 21:11 UTC
    Every named capture is numbered as well. So if you have a capture called 'foo', and it's the third capture, you find the offsets in $- [2] and $+ [2]. There isn't a hash with the indexes.
      It seems that the whole point of using named captures is not wanting to have to keep track of the position of those captures, so maybe this way of getting at it isn't so useful. Is there any programmatic way to translate from named captures to the corresponding numbers?
Re: Finding match offsets of named captures in 5.10 regex
by Anonymous Monk on Mar 03, 2010 at 18:21 UTC

    Obviously not a universal solution, but may be insertion of a (?{ code }) block could be helpful:

    $_ = 'I am sample'; while (m/(?<be>am)(?{say pos})/ig) { say $+{be}; }
Re: Finding match offsets of named captures in 5.10 regex
by LanX (Canon) on Jan 30, 2013 at 09:30 UTC
    > The following code maps names to numbers if the regex contains only named captures but falls apart if there are named ones too.

    > @n{re::regnames(1)} = (1 .. $#+);

    No sorry thats wrong, the names come in the wrong order.

    DB<108> '1234' =~ /(?<C>1)(?<D>2)(?<B>3)(?<A>4)/;re::regnames(1) => ("A", "D", "C", "B")

    Cheers Rolf

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://714041]
Approved by betterworld
Front-paged by betterworld
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2014-10-31 09:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (216 votes), past polls