问题
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