Course of Raku / Regexes and grammars / Regexes / Lookaround assertions / Exercises / A bare number

Solution: A bare number

Here is a possible solution to the task.

Code

say '5 and $9' ~~ / <!after '$'> \d+ /;

🦋 You can find the source code in the file dollar-amount.raku.

Output

「5」

Comments

  1. <!after '$'> is a negative lookbehind: it succeeds only when the character just before the current position is not a $. It checks that neighbour without consuming it.

  2. The 9 is rejected because it sits right after a $, so the engine matches the earlier 5 instead, where nothing precedes it.

Course navigation

A bare number   |   Not followed by