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
<!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.The
9is rejected because it sits right after a$, so the engine matches the earlier5instead, where nothing precedes it.
Course navigation
← A bare number | Not followed by →