$find = '$a'; $find = quotemeta $find; $replace = '$b'; $str = '($a $a hi) $a (($a) $a'; $str =~ s[(\([^)]*$find)|$find] [$1 ? $1 : $replace ]eg; print $str;
We find either (...$find or $find. When we find an opening parenthesis we move as far as we can to get $a (without passing a closing parenth) so that we do not substitute the second $a in the first parenth in my example. We then see if we have captured something in $1. If so we substitute what we found back in, effectively leaving the string unchanged. If not we do the replacement. You have not indicated how you want to handle mismatched or nested parentheses. This will not handle nesting 'correctly'. If you need to handle nesting you would do well to look at Text::Balanced
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
In reply to Re: Replacement Regex
by tachyon
in thread Replacement Regex
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |