my $str = "Foooboooobaaar"; while($str =~ /(\w)(\1+)/gi) { print "\nInside while concat_str -- $1 ---- $2 ---> $1.$2"; $str_concat = $1.$2; $str_rep_concat = $1.'*'; $str=~ s/$str_concat/$str_rep_concat/gi; } print "\n$str";