Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find number of matches to regexp in perl6?

Tags:

regex

raku

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?

like image 898
Daniel Avatar asked Apr 01 '18 08:04

Daniel


2 Answers

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.

like image 151
Kaiepi Avatar answered Feb 01 '23 19:02

Kaiepi


I found solution on RosettaCode.

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

say '01001011'.comb(/1/).elems;     #prints 4
like image 42
Daniel Avatar answered Feb 01 '23 19:02

Daniel