Metadata-Version: 2.4
Name: range_ex
Version: 0.0.6
Summary: Generate regex for numerical ranges
Author-email: Niels Mündler <n.muendler@posteo.de>, Raj Kiran P <rajkiranjp@gmail.com>
License: MIT
Project-URL: Homepage, https://github.com/nielstron/range-ex
Project-URL: Repository, https://github.com/nielstron/range-ex
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Development Status :: 4 - Beta
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: dev
Requires-Dist: pytest; extra == "dev"
Requires-Dist: pytest-xdist; extra == "dev"
Requires-Dist: hypothesis; extra == "dev"
Dynamic: license-file

range-ex
=======
[![Tests & QA](https://github.com/nielstron/range-ex/actions/workflows/tests.yml/badge.svg)](https://github.com/nielstron/range-ex/actions/workflows/tests.yml)
[![Coverage Status](https://coveralls.io/repos/github/nielstron/range-ex/badge.svg?branch=master)](https://coveralls.io/github/nielstron/range-ex?branch=master)
[![PyPI version](https://badge.fury.io/py/range-ex.svg)](https://pypi.org/project/range-ex/)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/range-ex.svg)
[![PyPI - Status](https://img.shields.io/pypi/status/range-ex.svg)](https://pypi.org/project/range-ex/)

This tool builds a regular expression for a numerical range.


### Installation

```sh
pip install range-ex
```


<!-- USAGE EXAMPLES -->
## 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.

```python
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:

```python
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.

```python
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 (`(?:...)`):

```python
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`:

```python
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`, `\d` matches any Unicode decimal digit (e.g. `٣`), while `[1-9]` only matches ASCII digits. Only use `ascii_digits=False` when the regex is compiled with `re.ASCII` or used with an engine where `\d` is 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:

```sh
pytest -n 5
```

### Acknowledgements

This project is based on [regex_engine](https://github.com/raj-kiran-p/regex_engine). Feel free to check it out.
