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

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";

Replies are listed 'Best First'.
Re^5: Does Perl support a given number of replacements ?
by johngg (Canon) on Jun 06, 2009 at 12:47 UTC

    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