Course of Raku / Regexes and grammars / Regexes / Matching strings / Exercises / Where is the digit
Solution: Where is the digit
Here is a possible solution to the task.
Code
my $m = 'abc9x2z' ~~ /\d/;
say $m.from;
say $m.to;🦋 You can find the source code in the file where-digit.raku.
Output
3
4Comments
The
\dclass matches a single digit. The engine stops at the first one it finds —9, at index3(positions count from zero); the later2is never reached..fromgives the start of the match,3, and.togives the position just after it ends,4. The difference.to - .fromis the length of the match — one character here.