【发布时间】:2018-09-10 18:47:32
【问题描述】:
在 Perl 5 中我们可以编写
my @things = $text =~ /thing/g;
标量上下文中的$things 是字符串$text 中子字符串thing 的非重叠出现次数。
如何在 Perl 6 中做到这一点?
【问题讨论】:
在 Perl 5 中我们可以编写
my @things = $text =~ /thing/g;
标量上下文中的$things 是字符串$text 中子字符串thing 的非重叠出现次数。
如何在 Perl 6 中做到这一点?
【问题讨论】:
我在RosettaCode找到了解决方案。
http://rosettacode.org/wiki/Count_occurrences_of_a_substring#Perl_6
say '01001011'.comb(/1/).elems; #prints 4
【讨论】:
say '01001011'.comb("1").elems(这大约快 15 倍,因为它不使用正则表达式引擎和很多很多 Match 对象创建)
你可以这样做:
my $text = 'thingthingthing'
my @things = $text ~~ m:g/thing/;
say +@things; # 3
~~ 将左侧与右侧匹配,m:g 使测试返回包含所有结果的 List[Match]。
【讨论】: