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}; #### abaacaa 0 2 3 5 6 aabaaaacaa