在Perl 5中我们可以写
my @things = $text =~ /thing/g;
Run Code Online (Sandbox Code Playgroud)
而$things在标量上下文是不重叠的子串出现的次数thing字符串$text.
在Perl 6中如何做到这一点?
你可以这样做:
my $text = 'thingthingthing'
my @things = $text ~~ m:g/thing/;
say +@things; # 3
Run Code Online (Sandbox Code Playgroud)
~~匹配左侧与右侧,m:g使测试返回List[Match]包含所有结果.
我找到了解决方案RosettaCode.
http://rosettacode.org/wiki/Count_occurrences_of_a_substring#Perl_6
say '01001011'.comb(/1/).elems; #prints 4
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
151 次 |
| 最近记录: |