Using some and any

If the number of digits is unspecified (but there is at least one digit), we can use some:

parse "XY67" ["XY" some digit]      ;== trueparse "XY" ["XY" some digit]        ;== falseparse "XY2915" ["XY" some digit]    ;== true

any can be used instead of some, matching the pattern even when there is no digit in the input:

parse "XY67" ["XY" any digit]   ;== trueparse "XY" ["XY" any digit]     ;== true

To summarize, some is 1 or more, any is 0 or more.

any and some only apply to the first item that follows them. For example:

parse "XXY" [some "X" "Y"]      ;== trueparse "XYXY" [some "X" "Y"]     ;== false

In the last line, the second "X" is not matched. To make this work, we could use the following:

parse "XYXY" [some ["X" "Y"]]    ;== true

Or even simpler: ...

Get Learn Red - Fundamentals of Red now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.