Exrex
Exrex is a Python library and command-line tool for 'reverse regular expressions,' designed to generate strings that match a given regular expression. It's useful for tasks like testing, fuzzing, and creating sample data. The library supports common regex features and handles potentially infinite matching strings by limiting their maximum length. It is a pure Python library with no external dependencies and efficiently uses generators to manage memory. The current version is 0.12.0, with an infrequent release cadence.
Common errors
-
ModuleNotFoundError: No module named 'exrex'
cause The `exrex` library has not been installed in your current Python environment.fixRun `pip install exrex` in your terminal to install the package. -
TypeError: 'generator' object is not subscriptable
cause You are trying to access elements from the `exrex.generate()` function directly using an index (e.g., `exrex.generate('...')[0]`) instead of iterating over it or converting it to a list.fixConvert the generator to a list if you need random access (e.g., `list(exrex.generate('...'))`) or iterate through it (e.g., `for s in exrex.generate('...'): print(s)`). -
exrex.py: error: argument REGEX: invalid regex
cause This error occurs when using `exrex` from the command line, indicating that the provided regular expression string is syntactically invalid or not correctly escaped for your shell.fixEnsure your regular expression is correctly formatted. If it contains special shell characters, enclose it in appropriate quotes (single quotes are usually safer on Unix-like systems, double quotes on Windows). Test the regex with Python's `re` module or an online regex validator first.
Warnings
- gotcha Generating all possible strings for complex or broad regular expressions can consume significant memory and time. Use the `limit` parameter (e.g., in `generate` or `count`) or prefer `getone` for efficiency, especially with infinite patterns like `.*` or `[a-z]+`.
- deprecated While PyPI classifiers for `exrex` list compatibility with Python 2.6, 2.7, and Python 3.3-3.5, these versions are officially end-of-life and unsupported. Relying on `exrex` with these older Python versions may lead to unpatched issues or compatibility problems with other modern libraries.
- gotcha The `exrex` library may not fully support all advanced or obscure features of Python's `re` module. While it handles most common regex patterns, certain complex constructs or specific flags might behave unexpectedly or not be fully implemented, leading to parsing errors or incorrect string generation.
Install
-
pip install exrex
Imports
- exrex
import exrex
- generate
import exrex.generate
from exrex import generate
Quickstart
import exrex
# Generate a single random string matching a regex
random_string = exrex.getone('[a-z]{3}\d{2,4}')
print(f"Random string: {random_string}")
# Generate all matching strings for a simple regex (up to a default limit)
all_strings = list(exrex.generate('(hello|world)!'))
print(f"All strings: {all_strings}")
# Count the number of matching strings
count = exrex.count('[01]{0,3}')
print(f"Count of [01]{0,3} matches: {count}")