如何计算Perl中的重叠子字符串? [英] How can I count overlapping substrings in Perl?
问题描述
我需要实现一个程序来计数在perl中的字符串中子串的出现。我已经实现如下
i need to implement a program to count the occurrence of a substring in a string in perl. i have implemented it as follows
sub countnmstr
{
$count =0;
$count++ while $_[0] =~ /$_[1]/g;
return $count;
}
$count = countnmstr("aaa","aa");
print "$count\n";
现在这是我通常会做的。然而,在上面的实现我想计算'aa'在'aaa'的发生。这里我得到答案为1这似乎合理,但我需要考虑重叠的情况下,以及。因此,上述情况应该给出答案为2,因为如果我们考虑重叠,有两个aa。
now this is what i would normally do. however, in the implementation above i want to count occurrence of 'aa' in 'aaa'. here i get answer as 1 which seems reasonable but i need to consider the overlapping cases as well. hence the above case should give an answer as 2 since there are two 'aa's if we consider overlap.
任何人都可以建议如何实现这样的功能。
can anyone suggest how to implement such a function??
推荐答案
请参阅 ysth的回答 ...我没有意识到该模式可以仅由零宽度断言组成,仍然可以用于此目的。
See ysth's answer ... I failed to realize that the pattern could consist solely of a zero width assertion and still work for this purpose.
您可以使用 positive lookahead ,并将函数写为:
You can use positive lookahead as suggested by others, and write the function as:
sub countnmstr {
my ($haystack, $needle) = @_;
my ($first, $rest) = $needle =~ /^(.)(.*)$/;
return scalar (() = $haystack =~ /(\Q$first\E(?=\Q$rest\E))/g);
}
您也可以使用 pos
,以调整下一次搜索的起点:
You can also use pos
to adjust where the next search picks up from:
#!/usr/bin/perl
use strict; use warnings;
sub countnmstr {
my ($haystack, $needle) = @_;
my $adj = length($needle) - 1;
die "Search string cannot be empty!" if $adj < 0;
my $count = 0;
while ( $haystack =~ /\Q$needle/g ) {
pos $haystack -= $adj;
$count += 1;
}
return $count;
}
print countnmstr("aaa","aa"), "\n";
输出:
C:\Temp> t
2
这篇关于如何计算Perl中的重叠子字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!