in reply to Re^3: Match a pattern only if it is not within another pattern
in thread Match a pattern only if it is not within another pattern
You can do the same thing but add Regexp::Common for arbitrary balanced delimiters;
use Regexp::Common; my $orig = my $str = 'foo bar foo bar foo qux foo qux foo'; $str =~ s{ ( $RE{balanced}{-begin => "bar"}{-end => "qux"} ) | (foo) } { defined $2 ? 123 : $1 }xge; print "$orig\n"; print "$str\n";
Result:
foo bar foo bar foo qux foo qux foo 123 bar foo bar foo qux foo qux 123
-xdg
Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.
|
|---|