in reply to Progressive matching w/substitutions
\G is like ^, but uses pos. And apparently, it works even without the "g" modifier present.
while ($string =~ /abc/ig) { my $ins = sprintf(" def(%s)", $x++); my $pos = $+[0]; $string =~ s/\G/$ins/; pos($string) = $pos + length($ins); }
But are you using the substitution operator at all?
while ($string =~ /abc/ig) { my $ins = sprintf(" def(%s)", $x++); my $pos = $+[0]; substr($string, $pos, 0, $ins); pos($string) = $pos + length($ins); }
Finally, this is really the task of the "e" modifier.
$string =~ s/(abc)/ sprintf("%s def(%s)", $1, $x++) /eig;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Progressive matching w/substitutions
by argv (Pilgrim) on Aug 09, 2008 at 16:27 UTC | |
by ikegami (Patriarch) on Aug 09, 2008 at 17:18 UTC | |
by argv (Pilgrim) on Aug 10, 2008 at 17:09 UTC | |
by tye (Sage) on Aug 10, 2008 at 17:17 UTC | |
by argv (Pilgrim) on Aug 10, 2008 at 21:33 UTC | |
|