I'm not sure if this problem solvable with regular expressions (in Perl5 syntax), but here is self-explanatory example:
smth Y1 test X foo X Y2 bar X Y1 X X Y2
s/?/Z/g
smth Y1 test Z foo Z Y2 bar X Y1 Z Z Y2
Consider that Y1 always have a matching Y2 and there is no overlapping.
Here you go:
$str = 'smth Y1 test X foo X Y2 bar X Y1 X X Y2';
$str =~ s/X(?=((?!Y1).)*Y2)/Z/g;
print $str; #smth Y1 test Z foo Z Y2 bar X Y1 Z Z Y2
A little awkward, but:
my $string = 'smth Y1 test X foo X Y2 bar X Y1 X X Y2';
$string =~ s/(Y1.*?Y2)/ (my $tmp = "$1") =~ tr!X!Z!; $tmp /ge;
print $string;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With