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