in reply to How do I replace certain character if condition exists
See. perlre and Regex::Common for more info on the regex used.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
_________
broquaint
|
|---|