in reply to perl search and replace

You can generalize it for any number of repeated characters too.

while ( my $string = <DATA> ) { $string =~ s/(.)(\1+)/$1.length($1.$2)/ge; print $string; } __DATA__ Balloon bookkeeper Wheeeee!!!

Output:

Bal2o2n bo2k2e2per Whe5!3