Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^4: Finding the right $<*digit*> capture variable

by hv (Prior)
on Apr 17, 2003 at 14:29 UTC ( [id://251215]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Re: Finding the right $<*digit*> capture variable
in thread Finding the right $<*digit*> capture variable

I think you can combine the information from @-, @+ and $^N to get most of the way there:

sub last_closed { my $last = $+[0]; my $len = length $^N; for (1 .. $#+) { return $_ if defined($+[$_]) && $+[$_] == $last && $+[$_] == $len + $-[$_]; } return undef; } "ad" =~ m{ ( (a) (?{ print last_closed() }) (b)? (?{ print last_closed() }) (c?) (?{ print last_closed() }) d ) (?{ print last_closed() }) }x;

This assumes a capture has just been closed; if that isn't necessarily the case, $last should instead be calculated as max(@+[1 .. $#+]).

Obviously this cannot report optional captures that did not capture; also, it can't tell the order of closing of empty strings - "c" =~ /((a*)b*)/ vs "c" =~ /(a*)(b*)/ - but if you are only interested in non-empty captures, I think this code will give the unique answer.

Hugo

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://251215]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2024-04-24 12:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found