Course of Raku / Regexes and grammars / Regexes / Alternations / Exercises / http or https
Solution: http or https
Here is a possible solution to the task.
Code
say 'https://example' ~~ / http | https /;🦋 You can find the source code in the file http-or-https.raku.
Output
「https」Comments
Both
httpandhttpscould match at the start, but|uses longest-token matching.So the longer
httpswins, even thoughhttpis written first.
An alternative solution
The two words differ only by a trailing s, so instead of
an alternation you can make that s optional with the
? quantifier:
say 'https://example' ~~ / https? /; # 「https」The ? binds only to the atom right before it — the
s — not to the whole word, so https? reads as
http followed by an optional s. That single
pattern matches both http and https, and being
greedy it takes the s whenever it is there.
Course navigation
← http or https | Anchors →