in reply to Efficient way to match and replace nested braces (etc.)

Another way of doing this is to build a regex with a finit number of nested levels:
my $text = 'this {{}}should be {{test {{ab{{c}}d}} zzzz}}good {{bw}}'; my $nested = 100; my $re = '{{(?:[^{}]+|'; $re .= $re x ($nested); $re .= ')*}}' x ($nested + 1); $text =~ s/$re//sg; print "$text\n"; __END__|OUTPUT: this should be good

Replies are listed 'Best First'.
Re^2: Efficient way to match and replace nested braces (etc.)
by Anonymous Monk on Apr 13, 2012 at 07:42 UTC
    Even better:
    my $re = '{{(?:[^{}]+|'; $re .= $re x ($nested) . "\b" . ')*}}' x ($nested + 1);