Course of Raku / Regexes and grammars / Regexes / Quantifiers / Exercises / An optional sign
Solution: An optional sign
Here is a possible solution to the task.
Code
say '-42' ~~ / '-'? \d+ /;🦋 You can find the source code in the file optional-sign.raku.
Output
「-42」Comments
The
'-'?part matches an optional minus sign — zero or one of them.\d+then matches the run of digits. The same pattern also matches a number with no sign, such as42.