in reply to all occurences of a target word by regular expression

perl -e '$string="apples and oranges are both fruit but I much prefer +apples"; @count=$string=~m/apple/g; print "\nThere are ", scalar @count," occurrences of the word apple in + the string\n";' There are 2 occurrences of the word apple in the string
Adapt your regex to suit. though I also have a problem seeing how you are count occurrences above

print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."

Replies are listed 'Best First'.
Re^2: all occurences of a target word by regular expression
by johngg (Canon) on Mar 30, 2010 at 21:41 UTC
    @count=$string=~m/apple/g; ... , scalar @count, ...

    You can get the count directly using this construct.

    $ perl -E ' > $str = q{I like apples, pears and pineapples}; > $count = () = $str =~ m{apple}g; > say $count;' 2 $

    I hope this is of interest.

    Cheers,

    JohnGG