http://qs1969.pair.com?node_id=1077596


in reply to Count number of occurrence of word

If you want to count all WORD occurrences within a string (but not within substrings), you can do the following using the goatse operator:

use strict; use warnings; my $string = 'How much wood could a woodchuck chuck if a woodchuck cou +ld chuck wood?'; my $woodCount = () = $string =~ /\bwood\b/g; print $woodCount; # prints 2

Hope this helps!