in reply to Re: Progressive matching w/substitutions
in thread Progressive matching w/substitutions
abc def(1)abc abc abc abc def(1)abc def(1)abc abc abc def(1)abc def(1)abc def(1)abc Done. string = 'abc def(1)abc def(1)abc def(1)abc'
Putting $x++; in your while loop after the substitution will fix the former, but not the latter.
Following on the other examples in this thread, one possibility is the following:
1:
my $string = "abc abc abc abc"; my $x = 1; # while ( $string =~ /abc /ig ) { while ( $string =~ /abc/ig ) { my $i = pos($string); # substr( $string, $-[0], $+[0]-$-[0]) =~ s/(abc) /$1 def($x)/; substr( $string, $-[0], $+[0]-$-[0]) =~ s/(abc)/"$1 def(" . $x++ . +")"/e; print "$string\n"; pos($string) = $i + 6; }
2:
my $string = "abc abc abc abc"; my $x = 1; # while ( $string =~ /(?=abc )/ig ) { while ( $string =~ /(?=abc)/ig ) { my $i = pos($string); # $string =~ s/\G(abc) /$1 def($x)/; $string =~ s/\G(abc)/"$1 def(" . $x++ . ")"/e; print "$string\n"; pos($string) = $i + 6; } print "\nDone. string = '$string'\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Progressive matching w/substitutions
by ikegami (Patriarch) on Aug 09, 2008 at 10:33 UTC | |
by broomduster (Priest) on Aug 09, 2008 at 10:50 UTC | |
by argv (Pilgrim) on Aug 09, 2008 at 16:47 UTC |