my $i = 0; *_ = \substr $str, $i while s/(.)(...)\1(?{ $i += pos() - 4 })/A$2B/;