Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: Count number of occurrence of word

by Kenosis (Priest)
on Mar 09, 2014 at 18:14 UTC ( #1077596=note: print w/replies, xml ) Need Help??

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!

Replies are listed 'Best First'.
Re^2: Count number of occurrence of word
by fattahsafa (Sexton) on Mar 09, 2014 at 18:17 UTC
    Perfect ! Thanks a works

      You're most welcome!

      See also perlsecret for more fathomless stuff. I didn't know that too :-(

      Update: Fixed typo...

      Regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1077596]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2023-02-05 20:32 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (32 votes). Check out past polls.