ikegami has asked for the wisdom of the Perl Monks concerning the following question:
What would be the best way of changing the following snippet so it prints (a,b)(c,d)(e,f)
use 5.010; # re features use strict; use warnings; for ('abcdef') { m{ (?(DEFINE) (?<x> ( . ) ( . ) (?{ '...' }) ) (?<y> ( . ) ( . ) (?{ print("(???,$^N)") }) ) (?<z> ( . ) ( . ) (?{ '...' }) ) ) (?&y)+ }x; } print("\n");
The most palatable solution I've found so far involves relying on the undocumented changing size of @- .
use 5.010; # re features use strict; use warnings; sub CAP { my ($idx) = $_[0]; $idx += @- if $idx < 0; # @+ isn't the same size as @- substr($_, $-[$idx], $+[$idx] - $-[$idx]) } for ('abcdef') { m{ (?(DEFINE) (?<x> ( . ) ( . ) (?{ '...' }) ) (?<y> ( . ) ( . ) # (?{ # printf('[%s,%s]', # ($#-), # Prints 6, the number of preceding captures # ($#+), # Prints 9, the total number of captures # ) # }) (?{ printf('(%s,%s)', CAP(-2), CAP(-1) ); }) ) (?<z> ( . ) ( . ) (?{ '...' }) ) ) (?&y)+ }x; } print("\n");
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: \g{-2} for inside (?{ ... })
by gone2015 (Deacon) on Nov 06, 2008 at 11:49 UTC | |
by ikegami (Patriarch) on Nov 06, 2008 at 12:32 UTC | |
|
Re: \g{-2} for inside (?{ ... })
by JavaFan (Canon) on Nov 06, 2008 at 13:31 UTC | |
by ikegami (Patriarch) on Nov 06, 2008 at 20:49 UTC | |
by tye (Sage) on Nov 07, 2008 at 02:39 UTC | |
by ikegami (Patriarch) on Nov 07, 2008 at 03:18 UTC | |
|
Re: \g{-2} for inside (?{ ... })
by Petras (Friar) on Nov 07, 2008 at 14:57 UTC | |
by wol (Hermit) on Nov 07, 2008 at 17:43 UTC |