Ranges
On this page
When you need to match a range of numbers, the range
syntax is your best friend. Character ranges
(e.g. ['0'-'7']
) are only able to match a single digit; the range
syntax has no
such limitation:
let octet = range '0'-'255';
# ipv4 address
octet ('.' octet){3}
This generates a regular expression that is both correct and as efficient as possible, since it
never requires backtracking. If you’re curious, here’s the regex the
range '0'-'255'
compiles to:
0|1[0-9]{0,2}|2(?:[0-4][0-9]?|5[0-5]?|[6-9])?|[3-9][0-9]?
Different bases
Pomsky can generate ranges in various bases. For example, to match hexadecimal numbers in a certain range, you might write:
range '10F'-'FFFF' base 16
Leading zeroes
If you wish to support leading zeros, this is easy to achieve by putting '0'*
in front:
'0'* range '0'-'1024'
If the number should have a certain length, with leading zeroes added when necessary, Pomsky has a special syntax for this:
range '0000'-'1024'
This matches numbers in the specified range with exactly 4 digits, such as 0110
or 0026
.