Perl,在由未知字符分隔的同一行中多次匹配一个模式 [英] Perl, match one pattern multiple times in the same line delimited by unknown characters
问题描述
我已经能够找到与此类似但不完全相同的问题.如何在由未知字符分隔的同一行中多次匹配一个正则表达式模式?
I've been able to find similar, but not identical questions to this one. How do I match one regex pattern multiple times in the same line delimited by unknown characters?
例如,假设我想匹配模式 HEY.我想识别以下所有内容:
For example, say I want to match the pattern HEY. I'd want to recognize all of the following:
嘿
嘿嘿
嘿xjfkdsjfkajHEY
HEYxjfkdsjfkajHEY
所以我在那里数了 5 个 HEY.所以这是我的程序,它适用于除最后一个之外的所有内容:
So I'd count 5 HEYs there. So here's my program, which works for everything but the last one:
open ( FH, $ARGV[0]);
while(<FH>)
{
foreach $w ( split )
{
if ($w =~ m/HEY/g)
{
$count++;
}
}
}
所以我的问题是如何替换那个 foreach 循环,以便我可以识别由未知配置中的奇怪字符分隔的模式(如上例所示)?
So my question is how do I replace that foreach loop so that I can recognize patterns delimited by weird characters in unknown configurations (like shown in the example above)?
感谢迄今为止的精彩回复.我刚刚意识到我还需要另一件事,我在下面发表了评论.
Thanks for the great responses thus far. I just realized I need one other thing though, which I put in a comment below.
一个问题:有没有办法保存匹配的术语?所以就像在我的情况下,有什么方法可以引用 $w(假设正则表达式更复杂,我想将它存储在出现次数的哈希中)
One question though: is there any way to save the matched term as well? So like in my case, is there any way to reference $w (say if the regex was more complicated, and I wanted to store it in a hash with the number of occurrences)
因此,如果我正在匹配一个真正的正则表达式(比如一个字母数字字符序列)并希望将其保存在哈希中.
So if I was matching a real regex (say a sequence of alphanumeric characters) and wanted to save that in a hash.
推荐答案
一种方法是捕获字符串的所有匹配项并查看您得到了多少.像这样:
One way is to capture all matches of the string and see how many you got. Like so:
open (FH, $ARGV[0]);
while(my $w = <FH>) {
my @matches = $w =~ m/(HEY)/g;
my $count = scalar(@matches);
print "$count\t$w\n";
}
是的,有!只需遍历所有匹配项,并使用捕获变量来增加哈希中的计数:
Yes, there is! Just loop over all the matches, and use the capture variables to increment the count in a hash:
my %hash;
open (FH, $ARGV[0]);
while (my $w = <FH>) {
foreach ($w =~ /(HEY)/g) {
$hash{$1}++;
}
}
这篇关于Perl,在由未知字符分隔的同一行中多次匹配一个模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!