in reply to Re: Incremental replacement by regex
in thread Incremental replacement by regex

:-)

I was sure I tried single quotes. But it was the e modifier that I needed. Thanks

This was my effort in the end

my $t = "This example could be another example where the example is th +e example I wanted\n"; my $count = 0; while($t =~ /example\s/){ my $example = "example_".++$count; $t =~ s/(example\s)/$example /; print $t; } print $t;

Replies are listed 'Best First'.
Re^3: Incremental replacement by regex
by cdarke (Prior) on Feb 21, 2011 at 14:18 UTC
    The 'g' in my example removes the need for the loop. One slightly pedantic comment on your code, you use \s and you should remember that it matches all whitespace, including a new-line. So, for example:
    my $t = "This example could be another example where the example is th +e example I wanted, for example\n";
    removes the trailing "\n" and replaces it with a space, which is probably not what you want.