My interpretive solution (in Perl):
$sample = 'smth Y1 test X foo X Y2 bar X Y1 XX Y2'; $sample =~ s/(?<=Y1) ((?:(?!Y1|Y2).)+) (?=Y2)/subX($1)/xeg; sub subX { ($str) = @_; $str =~ s/X/Z/g; return $str; } print $sample;
Output:
smth Y1 test Z foo Z Y2 bar X Y1 ZZ Y2
source share