Regex enumerator
This library is meant to generate all the strings that match a given regex pattern. It is written in python and uses no external libraries.
Installation
pip install regex-enumerator
Usage
Here's an example of how to use the library:
from regex_enumerator import RegexEnumerator
# Create a RegexEnumerator
re = RegexEnumerator(r'a[0-9]b')
# Get the next string that matches the regex
print(re.next()) # a0b
print(re.next()) # a1b
print(re.next()) # a2b
What is supported
- Character classes
- Quantifiers (greedy)
- Groups (named and unnamed)
- Alternation
- Escaped characters
- Backreferences (named and unnamed)
- Non-capturing groups
What I plan to support
I think those features would slow down the library too much and they are not widely used. If you have suggestions on how to implement them efficiently, please let me know.
- Lookahead
- Lookbehind
What is not supported
- Unicode properties
- Word boundaries
- Anchors
- Non-greedy quantifiers
Charset
The library supports ASCII characters by default. To handle Unicode characters, include them explicitly in your regex or define a custom character set.
from regex_enumerator import RegexEnumerator
# Directly in regex
regex_enum = RegexEnumerator(r'£')
print(regex_enum.next()) # £
# Using additional_charset
unicode_charset = [chr(i) for i in range(ord('¡'), ord('£'))]
unicode_charset = ['¡', '¢', '£']
unicode_charset = '¡¢£'
unicode_charset = ['¡¢', '£']
regex_enum = RegexEnumerator(r'.', additional_charset=unicode_charset)
result = []
while (char := regex_enum.next()) is not None:
result.append(char)
assert '¡' in result
assert '¢' in result
assert '£' in result
How it works
This library works by parsing the regex pattern into a tree structure. Once parsed, it performs a breadth-first search (BFS) on the tree to generate all matching strings. This ensures it does not get stuck on unbounded quantifiers for character classes or groups.
Tests
The library includes a comprehensive test suite. To run the tests, use the following command:
pytest
License
I don't know what license to use, so I'm going to use the MIT license. If you have any suggestions, please let me know.
Contributors
Feel free to contribute to this project. I'm open to suggestions and improvements.
Metadata
Release files for regex-enumerator 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| regex_enumerator-1.0.0.tar.gz | 13.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| regex_enumerator-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 22.3 kB
Release files / regex_enumerator-1.0.0.tar.gz
| Download URL | regex_enumerator-1.0.0.tar.gz |
|---|---|
| Size | 13.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
37f3e2820556bc265840adbc438318b72f0bddd275672fa7bcde209ee58bc198
|
|
BLAKE2b-256 checksum How to use checksums |
9a2698e996d2771fd78920a86f6fc8b0344d4a8ce16e5e53f8b9c8526316c573
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.12.8
|
Release files / regex_enumerator-1.0.0-py3-none-any.whl
| Download URL | regex_enumerator-1.0.0-py3-none-any.whl |
|---|---|
| Size | 8.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
738d57c9c08ba5ff9e6d91a31a5964ded4602cf23b351e188092695786197166
|
|
BLAKE2b-256 checksum How to use checksums |
781b2a2c0adfad0d4e9a9fd77940d16995f1f19809bf8f27e52a41385e381aac
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.12.8
|