Course of Raku / Regexes and grammars / Regexes / Lookaround assertions / Exercises / Not followed by
Solution: Not followed by
Here is a possible solution to the task.
Code
say so '50 dollars' ~~ / \d+ >> <!before '%'> /;🦋 You can find the source code in the file not-followed.raku.
Output
TrueComments
<!before '%'>is a negative lookahead: it succeeds only when the text right after the number is not a%.The
>>is a word boundary marking the end of the number, and it matters here. Without it, the greedy\d+would backtrack: on50% offit would give up the0and match just5— which is not followed by%— so the pattern would wrongly succeed.>>forces\d+to take the whole number, so the lookahead is tested at the real end. As a result50 dollarsgivesTrue, while50% offcorrectly givesFalse.