Skip to main content

Generate regex for numerical ranges

Project description

Range-Ex

Tests & QA Coverage Status PyPI version PyPI - Python Version PyPI - Status

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]\d|1\d|8\d)

regex2 = range_regex(-65, 12)
# (?:\-[1-9]|\-[2-5]\d|\-1\d|\-6[0-5]|\d|1[0-2])

If you pass only one bound, the other is unbounded:

regex3 = range_regex(minimum=5)
# (?:[5-9]|[1-9]\d(?:\d)*)

regex4 = range_regex(maximum=89)
# (?:\-[1-9](?:\d)*|\d|[2-7]\d|1\d|8\d)

regex5 = range_regex()
# (?:\-[1-9](?:\d)*|[1-9](?:\d)*|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](?:\d)*|1\.[0-5](?:\d)*)

regex7 = float_range_regex(0.5, 1.5, strict=False)
# (?:1|(?:0)?\.[5-9](?:\d)*|1\.[0-5](?:\d)*)

regex8 = float_range_regex(maximum="1.5", strict=True)
# (?:\-[1-9](?:\d)*\.\d(?:\d)*|(?:0)?\.\d(?:\d)*|1\.[0-5](?:\d)*)

regex9 = float_range_regex(strict=True)
# (?:(?:\-)?(?:0|[1-9](?:\d)*)\.\d(?:\d)*|(?:\-)?(?:0|[1-9](?:\d)*)\.|(?:\-)?\.\d(?:\d)*)

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]\d|\-1\d|\-6[0-5]|\d|1[0-2])

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

range_ex-0.0.4.tar.gz (13.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

range_ex-0.0.4-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file range_ex-0.0.4.tar.gz.

File metadata

  • Download URL: range_ex-0.0.4.tar.gz
  • Upload date:
  • Size: 13.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for range_ex-0.0.4.tar.gz
Algorithm Hash digest
SHA256 ee2d24088615bc0b3e1c339a88244cc31995fc997012cd9ecff638b5625e0309
MD5 fe4536f55e2df27c6be5df487997368e
BLAKE2b-256 dd9f97d89e0aa023e711e0825f96ad08c0e127a83ad818a726ae7f25e454eba2

See more details on using hashes here.

Provenance

The following attestation bundles were made for range_ex-0.0.4.tar.gz:

Publisher: python-publish.yml on nielstron/range-ex

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file range_ex-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: range_ex-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for range_ex-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 1daa6c0f59b84c6ad11cec1fd3b0a947538188d54fb282e07ba333e4379d4e9a
MD5 cde109a3b2d9d0880042ff19ec7e9d05
BLAKE2b-256 93b78f34e2002ea1cf6e1f6d45b0f021147e72bdd7e9e058b15be1f8ffc3bfa3

See more details on using hashes here.

Provenance

The following attestation bundles were made for range_ex-0.0.4-py3-none-any.whl:

Publisher: python-publish.yml on nielstron/range-ex

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page