in reply to Regex match and replace
You could also run the regular expression repeatedly, using the \K escape.substr($s, 1+index $s, 'bar') =~ s/bar/foo/g;
If you are running an outdated version of Perl, you can do something similar with back references:while ($var =~ s/bar.*?\Kbar/foo/) {1}
Update: Corrected code, as I had a weird brain fail on reading the spec.while ($var =~ s/(bar.*?)bar/$1foo/) {1}
#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 |