如何找到 perl6 中正则表达式的匹配数?

How to find number of matches to regexp in perl6?

在 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

你可以这样做:

my $text = 'thingthingthing'
my @things = $text ~~ m:g/thing/;
say +@things; # 3

~~ 将左侧与右侧匹配,m:g 使测试 return 成为包含所有结果的 List[Match]