Skip to main content

Generate regex for numerical ranges

Project description

Range-Ex

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

This tool builds a regular expression for a numerical range.

Installation

pip install range-ex

Usage

Pass a minimum and maximum value to the range_regex function to generate a regex that matches numbers in that range. The range is inclusive, meaning both the minimum and maximum values are included in the regex.

Supports integer numbers and negative range.

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])

Note: This will still find matches in strings like 1234 or abc25def53, so you may want to wrap it in ^ and $ to match the whole string or \b...\b to ensure word boundaries are matched.

If you only pass one of the two arguments, the other will be set to None, which means it will not be constrained. In this case, the regex will match any number that is greater than or equal to the minimum or less than or equal to the maximum.

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

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

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.1.tar.gz (6.5 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.1-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for range_ex-0.0.1.tar.gz
Algorithm Hash digest
SHA256 74406560b875de008d892b4383a5114b123b9c74befba08def55e1bdeb769deb
MD5 fc9ff10b567c1415439c60e612aa2780
BLAKE2b-256 293065c12c76c4dfbec2ba8f5f4136570f86a7ace0524bd59e9539de90ee3174

See more details on using hashes here.

Provenance

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

File metadata

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

File hashes

Hashes for range_ex-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4372b4e6b5dd165d9c4fa9acdb79cd1f7474b82ac5ecc4c59ba7e718a709aa54
MD5 f3b538120d57678091f4c0bf71cd1407
BLAKE2b-256 c744a78dec70e04de802c0844f9acff559a3668cd30ec2cbfe1acc16eeb527ff

See more details on using hashes here.

Provenance

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