How to find number of matches to regexp in perl6?

妖精的绣舞 提交于 2021-01-27 04:11:32

问题


In Perl 5 we can write

my @things = $text =~ /thing/g;

And $things in scalar context is number of non-overlapping occurrences of substring thing in string $text.

How to do this in Perl 6?


回答1:


You can do it like this:

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

~~ matches the left side against the right side, m:g makes the test return a List[Match] containing all the results.




回答2:


I found solution on RosettaCode.

http://rosettacode.org/wiki/Count_occurrences_of_a_substring#Perl_6

say '01001011'.comb(/1/).elems;     #prints 4


来源:https://stackoverflow.com/questions/49596244/how-to-find-number-of-matches-to-regexp-in-perl6

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!