Course of Raku / Regexes and grammars / Regexes / Anchors / Exercises / A whole word
Solution: A whole word
Here is a possible solution to the task.
Code
say 'this is fine' ~~ /<< is >>/;🦋 You can find the source code in the file whole-word.raku.
Output
「is」Comments
The
<<and>>anchors require a word boundary on each side ofis.The letters
isalso sit insidethis, but there they have no word boundary on the left, so the engine skips that occurrence and matches the standalone wordisinstead.Both candidates print the same
「is」, so how do you know which one actually matched? Change theishidden insidethis— for example, turn the string into'thus is fine'. The output is still「is」, unchanged, proving that occurrence was never the match. Now change the standalone word instead (say,'this it fine') and the match disappears (Nil). You can also ask the match where it starts:('this is fine' ~~ /<< is >>/).fromreturns5, the position of the standaloneis, not2.