如何计算 Perl 中重叠的子字符串? [英] How can I count overlapping substrings in Perl?
问题描述
我需要实现一个程序来计算 perl 中字符串中子字符串的出现次数.我已经实现如下
sub countnmstr{$count = 0;$count++ 而 $_[0] =~/$_[1]/g;返回 $count;}$count = countnmstr("aaa","aa");打印 "$count
";
现在这是我通常会做的.但是,在上面的实现中,我想计算aaa"中aa"的出现次数.在这里,我得到的答案为 1,这似乎是合理的,但我也需要考虑重叠的情况.因此,上述情况应给出答案为 2,因为如果我们考虑重叠,则有两个aa".
谁能建议如何实现这样的功能??
参见 ysth 的回答 ...我没有意识到该模式可以仅由零宽度断言组成,并且仍然可以用于此目的.
您可以按照其他人的建议使用 positive lookahead,并将函数写为:
sub countnmstr {我的 ($haystack, $needle) = @_;我的 ($first, $rest) = $needle =~/^(.)(.*)$/;返回标量 (() = $haystack =~/(Q$firstE(?=Q$restE))/g);}
您还可以使用 pos
来调整位置下一个搜索从:
#!/usr/bin/perl使用严格;使用警告;子计数nmstr {我的 ($haystack, $needle) = @_;我的 $adj = length($needle) - 1;die "搜索字符串不能为空!"如果 $adj <0;我的 $count = 0;而 ( $haystack =~/Q$needle/g ) {pos $haystack -= $adj;$count += 1;}返回 $count;}打印 countnmstr("aaa","aa"), "
";
输出:
<前>C:Temp> t2i 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
";
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??
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.
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$firstE(?=Q$restE))/g);
}
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"), "
";
Output:
C:Temp> t 2
这篇关于如何计算 Perl 中重叠的子字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!