$_ = 'abxy' x 15; for(my $i = 0; m/ab/g and $i++<5;) { substr($_, $-[0], $+[0]-$-[0]) = 'ABC'; } print;