Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: How do I replace certain character if condition exists

by broquaint (Abbot)
on Apr 17, 2003 at 13:48 UTC ( [id://251199]=note: print w/replies, xml ) Need Help??


in reply to How do I replace certain character if condition exists

Regexp::Common to the rescue!
use strict; use Regexp::Common; my @tests = ( 'foo(bar)', 'foo bar', 'foo(bar', 'foo bar)', 'foo((bar)', 'foo(bar))', 'foo((bar)))', ); for(@tests) { printf "%12s", $_; s{ [()] }( )xg and tr/ //s unless m/ (?<! [(]) $RE{balanced}{-parens => '()'} (?! [)] ) /x; printf " - %s\n", $_; } __output__ foo(bar) - foo(bar) foo bar - foo bar foo(bar - foo bar foo bar) - foo bar foo((bar) - foo bar foo(bar)) - foo bar foo((bar))) - foo bar
See. perlre and Regex::Common for more info on the regex used.
HTH

_________
broquaint

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-25 20:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found