$Code = "BCBCBCBCBCCCB"; $What = "BC"; $Code =~ m/($What)+?/; $Exp = length ( $1 ) / length ( $What ); $Code =~ s/$1/($What)^($Exp)/e;