$count++ while ($str=~ /$pattern/g); # simple #### $count= (scalar split /$pattern/, $str ) + ($str=~/$pattern$/) # or it will not be counted - 1; # so it's simpler