在 Perl 5 中我们可以写
my @things = $text =~ /thing/g;
和
$things
在标量上下文中是子串的非重叠出现次数 thing
在字符串中 $text
.如何在 Perl 6 中做到这一点?
最佳答案
你可以这样做:
my $text = 'thingthingthing'
my @things = $text ~~ m:g/thing/;
say +@things; # 3
~~
将左侧与右侧匹配,m:g
使测试返回 List[Match]
包含所有结果。
关于regex - 如何在perl6中找到与regexp匹配的数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49596244/