Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^2: regular expression paranthesis remover

by Hofmator (Curate)
on Jun 25, 2004 at 09:19 UTC ( [id://369580]=note: print w/replies, xml ) Need Help??


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):

$_ = "111(22(33)44)55"; 1 while s/ \( [^()]* \) //gx; if (/[()]/) { print "unbalanced!!"; } else { print; }
BTW, I just noticed a small typo in Zaxo's code, it doesn't work as it stands. It should be my $re = qr/.../;.

-- Hofmator

Replies are listed 'Best First'.
Re^3: regular expression paranthesis remover
by thospel (Hermit) on Jun 25, 2004 at 12:26 UTC
    Fails on the perfectly balanced "12(34)56(78)9"

    Assuming it's about removing balanced parenthesis, I'd go for:

    my $balance; # Notice declaring $balance beforehand is important, # otherwise you pick up the global inside (??{}) $balance=qr/(?:[^()]|\((??{$balance})\))*/; $string =~ s/\($balance\)//g;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://369580]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-25 23:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found