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.5.tar.gz (14.6 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.5-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: range_ex-0.0.5.tar.gz
  • Upload date:
  • Size: 14.6 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.5.tar.gz
Algorithm Hash digest
SHA256 01e9b9c4d74891ee97e89de81f06b2a6ff8f3617293cf20858bf7b89f9647542
MD5 eff88d2761e12e90dc99d83c8b1e7332
BLAKE2b-256 44882d191f3d0b36d8b918d00b1d95219c3bd4a567d51fbae951e0788f83e89c

See more details on using hashes here.

Provenance

The following attestation bundles were made for range_ex-0.0.5.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.5-py3-none-any.whl.

File metadata

  • Download URL: range_ex-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 11.3 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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 9bd2c590aa7b9350bc3e1f352229290b0e9986068940073eb25a441e5eb4134f
MD5 827975c10c2fe842fd8f2892e2b13d07
BLAKE2b-256 1cec09ea712808d08a0d3963942201631d8f93f0e824bab07eb8887320f0a8f6

See more details on using hashes here.

Provenance

The following attestation bundles were made for range_ex-0.0.5-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