to-regex-range
Generate a compact regex that matches an integer range — (1, 99) →
[1-9]|[1-9][0-9]. A faithful Rust port of the
to-regex-range npm package (a
micromatch building block). Zero dependencies and #![no_std].
use ;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
let opts = default.shorthand;
assert_eq!;
Why to-regex-range?
Matching "a number between 1 and 255" with a regex by hand is tedious and
error-prone. This builds the minimal alternation for you — splitting the range at
power-of-ten boundaries and turning each sub-range into a character class with the
right quantifier. The result is a regex source string; wrap it in your engine of
choice (regex, fancy-regex, …).
[]
= "0.1"
API
| Item | Purpose |
|---|---|
to_regex_range(min, max) |
The regex source for the range, default options |
to_regex_range_with_options(min, max, &Options) |
…with options |
Options { capture, shorthand, wrap } |
capture → ( … ); shorthand → \d; wrap (default) → (?: … ) |
Behavior
min/maxmay be given in either order; equal bounds yield the literal number.- Negative ranges are handled, sharing a
-?prefix where positive and negative sub-patterns coincide. - The output is a bare regex source — no anchors or delimiters. Add
^(?:…)$if you need to match a whole string. - Integer ranges only; zero-padded ranges (e.g.
001..100) are out of scope.
Contributors ✨
This project follows the all-contributors specification. Contributions of any kind are welcome — code, docs, bug reports, ideas, reviews! See the emoji key for how each contribution is recognized, and open a PR or issue to get involved.
Thanks goes to these wonderful people:
License
Licensed under either of Apache-2.0 or MIT at your option.