in reply to regular expression paranthesis remover
If you match an open paren, increment a counter;my $c=0; s/ \( (?{++$c}) | \) (?{--$c}) | ([^()]+) /$1 if ($c==0)/gex;
There is an "if", but it can be replaced by an "x" operator, if you're a stickler. Or you could rewrite the replacement as $c==0 and $1.
Another little variation that doesn't rely on the (?{}) construct:
Match any parens followed by any non-parens;s{([()]*)([^()]+)} {$c += $1=~y/(// - $1=~y/)//; $2 x !$c }ge;
|
|---|