Course of Raku / Regexes and grammars / Regexes / Adverbs / Exercises / Count the matches

Solution: Count the matches

Here is a possible solution to the task.

Code

say ('education' ~~ m:g/<[aeiou]>/).elems;

🦋 You can find the source code in the file count-matches.raku.

Output

5

Comments

  1. The character class <[aeiou]> matches one vowel, and the :g adverb returns every such match instead of just the first.

  2. The result behaves like a list, so .elems counts the matches: the five vowels in education (e, u, a, i, o).

Course navigation

Count the matches   |   All the numbers