in reply to Re: Count number of occurrence of word
in thread Count number of occurrence of word

This node falls below the community's minimum standard of quality and will not be displayed.
  • Comment on Re^2: Count number of occurrence of word

Replies are listed 'Best First'.
Re^3: Count number of occurrence of word
by Laurent_R (Canon) on Mar 09, 2014 at 18:17 UTC

    Then show your test, because it does work:

    $ perl -e 'my $string = "foobarfoofoobarfoo"; my $count = $string =~ s +/foo/foo/g; print $count;' 4
    As you can see in this Perl one-liner, I print successfully the number of occurrences of "foo" in my $string.

    Or, perhaps I didn't understand part of your requirement. What do you mean by substring? Possibly you want this:

    my $count = $string =~ s/\bfoo\b/foo/g;

    Edit: Yes, from your answer to Kenosis, I see now that I had not understood what you meant when talking about substring, sorry. The last solution I posted above should work in accordance with your needs.

      Thanks alot. The second solution my $count = $string =~ s/\bfoo\b/foo/g; works well :)
        You are welcome.