in reply to Re: substitute with recursive regexps?
in thread substitute with recursive regexps?
which gives (with perl 5.10.0):use strict; use warnings; use Data::Dumper; $^R=undef; my $re = qr/ (?{[$^R]}) # initialize a new node in the par +setree # preserving the old as the 0th +element \( # match a paren (?> # atomic match ([^()]+) # capture (?{ push @{$^R},$1; # push the capture string into the + current node $^R # propagate the current node }) | (?0) # recurse (?{ push @{$^R->[0]},$^R; # push the current node into the o +ld node shift @{$^R} # and restore the old node to be t +he current # (while removing it from the new) }) )* \) /x; "(a(b)(c(d)e)(f)g)" =~ m/$re/x and print "matched\n"; print Dumper $^R;
But my current goal is not parsing, it is optimization of stringified output from parsed productions. It would have been cool to do optimizing transforms with recursive pattern substitutions. Then optimizations could be applied on the most nested level first, and propagate to higher levels.matched $VAR1 = [ undef, 'a', [ 'b' ], [ 'c', [ 'd' ], 'e' ], [ 'f' ], 'g' ];
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: substitute with recursive regexps?
by Anonymous Monk on Sep 04, 2011 at 21:14 UTC |