Is there a Perl shortcut to count the number of matches in a string?

That puts the regex itself in scalar context, which isn’t what you want. Instead, put the regex in list context (to get the number of matches) and put that into scalar context.

 my $number = () = $string =~ /\./gi;

Leave a Comment