in reply to Re: Getting the number of times a regexp matches
in thread Getting the number of times a regexp matches
$str = 'ababbb'; $pattern = 'b'; $count = (scalar split /$pattern/, $str ) + ($str=~/$pattern$/) - 1; print "$count\n";
Fortunately, this is an easy problem to fix:2
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.$count = (scalar split /$pattern/, $str, -1) - 1;
|
|---|