I managed to find similar, but not identical questions to this. How to match one regular expression pattern several times in the same line marked with unknown characters?
For example, let's say I want to combine a HEY pattern. I would like to know all of the following:
HEY
Hey hey
HEYxjfkdsjfkajHEY
So, I would count 5 HEY. So here is my program that works for everything but the last:
open ( FH, $ARGV[0]); while(<FH>) { foreach $w ( split ) { if ($w =~ m/HEY/g) { $count++; } } }
So my question is how to replace this foreach loop so that I can recognize patterns limited to strange characters in unknown configurations (as shown in the example above)?
EDIT:
Thanks for the great answers. I just realized that I needed one more thing that I added in the comments below.
One question: is there a way to keep a consistent term? As in my case, is there a way to refer to $ w (let's say if the regex was more complex and I wanted to keep it in a hash with the number of occurrences)
So, if I matched a real regular expression (say, a sequence of alphanumeric characters) and wanted to store it in a hash.
source share