Course of Raku / Regexes and grammars / Regexes / Quantifiers / Exercises / Two to four digits

Solution: Two to four digits

Here is a possible solution to the task.

Code

say 'abc12345' ~~ / \d ** 2..4 /;

🦋 You can find the source code in the file three-digits.raku.

Output

「1234」

Comments

  1. \d ** 2..4 matches between two and four digits in a row.

  2. The string has five digits available, but the range caps the match at four, so the pattern takes 1234 and leaves the final 5. Being greedy, it takes the most the range permits rather than the fewest.

  3. Greed only applies within a single match — the engine still stops at the first place the pattern succeeds. Even if a longer digit sequence appears later in the string, it is never reached:

    say 'abc123def6789012z' ~~ / \d ** 2..4 /;   # 「123」

    Here 123 is matched even though the later run 6789012 would have filled the whole range with 6789. The leftmost match wins.

Course navigation

Two to four digits   |   A frugal match