in reply to Regex match and replace

Is there a reason you need to do it as a compound regular expression? It'll probably be easier to do it as a compound operation with index, substr and a substitution:
substr($s, 1+index $s, 'bar') =~ s/bar/foo/g;
You could also run the regular expression repeatedly, using the \K escape.
while ($var =~ s/bar.*?\Kbar/foo/) {1}
If you are running an outdated version of Perl, you can do something similar with back references:
while ($var =~ s/(bar.*?)bar/$1foo/) {1}
Update: Corrected code, as I had a weird brain fail on reading the spec.

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Replies are listed 'Best First'.
Re^2: Regex match and replace
by Anonymous Monk on Mar 04, 2015 at 22:25 UTC

    Thank you for your detailed answer, that seems like an interesting way of doing it. Thanks!