in reply to Replacement Regex
$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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Replacement Regex
by danger (Priest) on Mar 20, 2002 at 19:06 UTC | |
by tachyon (Chancellor) on Mar 20, 2002 at 19:29 UTC | |
by I0 (Priest) on Mar 22, 2002 at 00:34 UTC | |
|
Re: Re: Replacement Regex
by Anonymous Monk on Mar 20, 2002 at 19:40 UTC |