Course of Raku / Regexes and grammars / Grammars / Creating grammars / Exercises / Parse a hashtag

Solution: Parse a hashtag

Here is a possible solution to the task.

Code

grammar Hashtag {
    token TOP { '#' <tag> }
    token tag { \w+ }
}

say Hashtag.parse('#raku')<tag>;

🦋 You can find the source code in the file parse-greeting.raku.

Output

「raku」

Comments

  1. TOP spells out the fixed # followed by the <tag> token.

  2. .parse requires the whole string to match, and the tag is then available as the <tag> capture.

Course navigation

Parse a hashtag   |   Parse a time