in reply to Re: Does Perl support a given number of replacements ?
in thread Does Perl support a given number of replacements ?

or, a little more concisely...

$s =~ s/a/B/ for(1..3);

Replies are listed 'Best First'.
Re^3: Does Perl support a given number of replacements ?
by AnomalousMonk (Archbishop) on Jun 06, 2009 at 05:50 UTC
    I think ig's reply and also Re: Does Perl support a given number of replacements ? have a bug. Consider:
    >perl -wMstrict -le "my $x = 'aBaCaDaEaFa'; $x =~ s/a/aa/ for 1 .. 3; print $x; "
    If I run this code, I might expect  'aaBaaCaaDaEaFa' to be printed, but I get  'aaaaBaCaDaEaFa' instead. This is because the regex of the substitution does not 'remember' its position in the string being searched, but instead starts from the beginning of the string on each invocation. So, if the substituted portion of the string contains anything that may be matched by the regex, then it, and not anything further along in the string, is what will be (repeatedly) replaced.

    jwkrahn's reply Re: Does Perl support a given number of replacements ? shows a way around this.

    Update: Improved first example and changed pertinent discussion, removed a second example as no longer offering greater clarity.

      Quite right!!

      I experimented with the g option, \G and pos() but they don't work with substitutions as they do with matches and in the end I couldn't find any RE based solution simpler than jwkrahn's that handles replacements that match the search pattern.

      But I find REs difficult enought to understand without embeded code, so I came up with the following alternative which may have merit in some situations. It works for the examples you provided but it's not obvious that its doing replacement and the third argument to split is one more than the number of replacements, so it's certainly not ideal.

      my $s1 = 'abaacaa'; my $s2 = 'abcdefg'; $s1 = join('aa', split('a', $s1, 4)); $s2 = join('aa', split(/[abcedfg]/, $s2, 4)); print "$s1\n"; print "$s2\n";

        Another way would be to combine a regular expression look-ahead with pos to find all of the potential positions in the text where a substitution might be made and then use substr to make the substitutions, working from the last substitution forwards to avoid the positions going out of kilter.

        use strict; use warnings; my $text = q{abaacaa}; my $repCt = 3; my $replace = q{a}; my $len = length $replace; my $with = q{aa}; print qq{$text\n}; my @posns; push @posns, pos $text while $text =~ m{(?=$replace)}g; print qq{@posns\n}; substr $text, $_, $len, $with for reverse @posns[ 0 .. $repCt - 1 ]; print qq{$text\n};

        The output.

        abaacaa 0 2 3 5 6 aabaaaacaa

        I hope this is of interest.

        Cheers,

        JohnGG

Re^3: Does Perl support a given number of replacements ?
by Anonymous Monk on Jun 06, 2009 at 01:29 UTC
    Wonderful !!! That's exactly what I was thinking of :)) Thanks.