in reply to Re: Getting the number of times a regexp matches
in thread Getting the number of times a regexp matches

The split solution is not correct, because it does not account for multiple occurences of $pattern at the end of the string:
$str = 'ababbb'; $pattern = 'b'; $count = (scalar split /$pattern/, $str ) + ($str=~/$pattern$/) - 1; print "$count\n";
2
Fortunately, this is an easy problem to fix:
$count = (scalar split /$pattern/, $str, -1) - 1;
The third argument to split specifies the maximum number of pieces to split the string into. A negative number turns off the stripping of null fields from the end of the list, without limiting the number of pieces.