in reply to Substituting a comma only when it is preceded and followed by a bracket
This assumes the parenthesis are balanced.my $str = do {my $i; join "", map {$_ eq '(' ? $i++ : $_ eq ')' && $i +? $i-- : $_ eq ',' && $i ? ($_ = ';') : 0; $_} split '[(,)]', $str};
But what should happen on:
? The comma here is preceded and followed by a bracket - preceded by an opening paren, and followed by a closing paren even. If such a comma should be replaced as well, one might use something like (untested):"(foo),(bar)"
substr($str, index($str, "("), rindex($str, ")") - length($str)) =~ s/ +,/;/g;
|
|---|