foreach my $a(@arr) { $a =~ s/^\s*//; #trim leading spaces $a =~ s/\s*$//; #trim trailing spaces my $count = grep {$_ =~ /($a)/g} @text; print "\'$a\' found $count times\n"; }
The scalar form of grep returns the count of "matches" - not just a "true" value.
I think that is the main point that you missed.
If @arr contains multiple words, then I'm not know what you mean by a "match". I just trimmed the leading and trailing spaces from $a. If you have things in @arr like "Bob and Mary", and you want that to match across lines in @text, that is different too. But this will find " Bob and Mary " in an single line.
Your requirement is not well specified. Better would be to show some input and desired output.
In reply to Re: REGEX:getting the count of occerence
by Marshall
in thread REGEX:getting the count of occerence
by ansh batra
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |