$string ='ab23cdefgXX(3A5)XXhijkl23mnXX(3)XXopq432rsXX(450b)XXtuv'; $count=0; while($string =~ m/XX/g) { $count++; } print $count;;