use strict; use warnings; my $string = "aabbcc"; if($string =~ m{^(aabb)(.*)$}) { $string = substr($1,0,length($1)-1) . "X" . $2; } print $string;