in reply to Re: regular expression paranthesis remover
in thread regular expression paranthesis remover
That's needlessly complicated, simply replace the stuff you don't want with nothing (as Anonymous Monk already said): s/\(.*\)//.
The balancing caveat still applies, of course.
Update: thospel is completely correct, my solution above (and Zaxo's) doesn't work for multiple parenthesis in the same string. To fix this (and actually make it recognise balanced parens):
BTW, I just noticed a small typo in Zaxo's code, it doesn't work as it stands. It should be my $re = qr/.../;.$_ = "111(22(33)44)55"; 1 while s/ \( [^()]* \) //gx; if (/[()]/) { print "unbalanced!!"; } else { print; }
-- Hofmator
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: regular expression paranthesis remover
by thospel (Hermit) on Jun 25, 2004 at 12:26 UTC |