Skip to main content

Enumerate all strings that match a given regex

Project description

Regex enumerator

PyPI version

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.

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

regex_enumerator-1.0.0.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

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

regex_enumerator-1.0.0-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file regex_enumerator-1.0.0.tar.gz.

File metadata

  • Download URL: regex_enumerator-1.0.0.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for regex_enumerator-1.0.0.tar.gz
Algorithm Hash digest
SHA256 37f3e2820556bc265840adbc438318b72f0bddd275672fa7bcde209ee58bc198
MD5 bbba630e36c70efc3a3dbbc2200236d1
BLAKE2b-256 9a2698e996d2771fd78920a86f6fc8b0344d4a8ce16e5e53f8b9c8526316c573

See more details on using hashes here.

File details

Details for the file regex_enumerator-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for regex_enumerator-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 738d57c9c08ba5ff9e6d91a31a5964ded4602cf23b351e188092695786197166
MD5 e1f7858e03efaff550a1880454d33ca1
BLAKE2b-256 781b2a2c0adfad0d4e9a9fd77940d16995f1f19809bf8f27e52a41385e381aac

See more details on using hashes here.

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