use strict; use warnings; use 5.010; my $string = 'foobar'; while ($string =~ s/\G.*?\K[aeiou]/\U$&/si) { say $&; } continue { pos($string) = $+[-1]; } say $string;
The trick is to manually set pos, and then use \G in the regex to anchor to that position. But since you want to match anything after that (and not just exactly at the current position), you need a .*? (and the /s modifier if newlines are in the string).
Now it matches too much text, but the \K assertion cuts off the start of the match, making $& what it should be.
\K requires perl 5.10.0, but since that's the third-oldest major perl 5 release, that shouldn't be a problem. It's not 1995 after all :-)
Insert generic warning about the runtime overhead of $& here, and how it's better to extract it with substr, @+ and @-.
In reply to Re: Loop through global substitution
by moritz
in thread Loop through global substitution
by alafarm
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |