range-ex
This tool builds a regular expression for a numerical range.
Installation
pip install range-ex
Usage
Use range_regex for integer ranges and float_range_regex for decimal ranges.
Integer ranges (range_regex)
range_regex matches only integers (including negatives). The range is inclusive on both ends.
from range_ex import range_regex
regex1 = range_regex(5, 89)
# (?:[5-9]|[2-7][0-9]|1[0-9]|8[0-9])
regex2 = range_regex(-65, 12)
# (?:\-[1-9]|\-[2-5][0-9]|\-1[0-9]|\-6[0-5]|[0-9]|1[0-2])
If you pass only one bound, the other is unbounded:
regex3 = range_regex(minimum=5)
# (?:[5-9]|[1-9][0-9]+)
regex4 = range_regex(maximum=89)
# (?:\-[1-9][0-9]*|[0-9]|[2-7][0-9]|1[0-9]|8[0-9])
regex5 = range_regex()
# (?:\-[1-9][0-9]*|[1-9][0-9]*|0)
Decimal ranges (float_range_regex)
float_range_regex accepts int, float, Decimal, and decimal-parseable str bounds.
from range_ex import float_range_regex
regex6 = float_range_regex(0.5, 1.5, strict=True)
# (?:0?\.(?:[5-9]|[5-9][0-9]*)|1\.(?:(?:[0-5]|[0-4][0-9]*|50+))?|1\.)
regex7 = float_range_regex(0.5, 1.5, strict=False)
# (?:0?\.(?:[5-9]|[5-9][0-9]*)|1\.(?:(?:[0-5]|[0-4][0-9]*|50+))?|1\.?)
regex8 = float_range_regex(maximum="1.5", strict=True)
# (?:\-(?:(?:0|[1-9][0-9]*)\.[0-9]+|(?:0|[1-9][0-9]*)\.|\.[0-9]+)|0?\.[0-9]+|0\.[0-9]*|1\.(?:(?:[0-5]|[0-4][0-9]*|50+))?|(?:\-[1-9][0-9]*|[0-1])\.|\-0\.|\-0\.0+|\-\.0+)
regex9 = float_range_regex(strict=True)
# (?:\-?(?:0|[1-9][0-9]*)\.[0-9]+|\-?(?:0|[1-9][0-9]*)\.|\-?\.[0-9]+|(?:\-[1-9][0-9]*|[1-9][0-9]*|0)\.|\-0\.|\-0\.0+|\-\.0+)
Rendering options
Both APIs support capturing=True to emit capturing groups ((...)) instead of non-capturing groups ((?:...)):
range_regex(-65, 12, capturing=True)
# (\-[1-9]|\-[2-5][0-9]|\-1[0-9]|\-6[0-5]|[0-9]|1[0-2])
By default, digits are written as ASCII character classes ([0-9], [1-9], ...).
Both APIs support ascii_digits=False to write [0-9] as the shorter \d:
range_regex(-65, 12, ascii_digits=False)
# (?:\-[1-9]|\-[2-5]\d|\-1\d|\-6[0-5]|\d|1[0-2])
Warning: In Python's
re,\dmatches any Unicode decimal digit (e.g.٣), while[1-9]only matches ASCII digits. Only useascii_digits=Falsewhen the regex is compiled withre.ASCIIor used with an engine where\dis ASCII-only.
Note: Generated regexes can still match inside larger strings (for example, inside
abc25def). Use anchors like^...$if you need whole-string matches.
Contributing
Contributions are very welcome. Please open an issue or a pull request if you have any suggestions or improvements.
To test your changes, run the following command:
pytest -n 5
Acknowledgements
This project is based on regex_engine. Feel free to check it out.
Metadata
Release files for range-ex 0.0.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| range_ex-0.0.6.tar.gz | 15.7 kB | With provenance | Details for range_ex-0.0.6.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| range_ex-0.0.6-py3-none-any.whl | Python 3 | none | any | Details for range_ex-0.0.6-py3-none-any.whl |
Total release size: 27.6 kB
Release files / range_ex-0.0.6.tar.gz
| Download URL | range_ex-0.0.6.tar.gz |
|---|---|
| Size | 15.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
79d26b3999a10c8f71115fc0c3e22d1a65c4bf8e10328a4494d004b2c7126d0c
|
|
BLAKE2b-256 checksum How to use checksums |
8fb9df336c3163856bab2779b6f55a92dc2588d8ed73a4c684c015fc47c405d7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logRelease files / range_ex-0.0.6-py3-none-any.whl
| Download URL | range_ex-0.0.6-py3-none-any.whl |
|---|---|
| Size | 11.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1858d8c0660f6556a51ff6f3a33cbff4fdc619c892015fc68abeec27216314bb
|
|
BLAKE2b-256 checksum How to use checksums |
9d97ccafeab45b865a55dafdbabc1a9a0444d9c17eb33b77afbec5deb4684348
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logSecurity for range-ex 0.0.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for range_ex-0.0.6.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.