in reply to counting the number of occurrances of a word using regex
Probably tr isn't what you want here, and there's no need to capture the word, so no parentheses. A simple solution is:
my @count = $str =~ /\bthe\b/gi; print scalar @count, "\n";
The idea is that g searches globally while i makes the search case-insensitive. Assigning the results to an array means that the @count is populated with all of the successful matches. Placing the array in scalar context will return the number of matches.
|
|---|