Registry / testing / exrex
library0.12.0pypypi✓ verified 87d ago

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.

pip install exrex
INSTALL
IMPORT
SIG · EXREX
E
exrex
testingpythonv0.12.0
Install
1.5s avg
Import
8ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.12.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.008s · 17.8MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.5s · import 0.002s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

exrex
import exrex
The primary module is typically imported directly.
generate
from exrex import generate
import exrex.generate
Functions like 'generate' are members of the exrex module, not submodules.

The quickstart demonstrates generating a single random string, iterating through all possible matching strings (up to an internal limit for infinite regexes), and counting the total number of matches for a given regular expression.

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}")
exrex --version
Debug
Known issues
gotchaGenerating 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]+`.
fix
Always consider the potential output size. For `generate`, wrap in `list()` with caution, or iterate directly. Use `exrex.getone()` for a single random match. For patterns with quantifiers like `+` or `*`, `exrex` applies an internal limit to generated length to prevent infinite loops and excessive output, but this may not cover all memory/performance implications.
affects: All
deprecatedWhile 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.
fix
It is strongly recommended to use `exrex` with Python 3.6 or newer for better security, performance, and compatibility within modern Python environments. The library explicitly states Python 3.3+ compatibility, but best practice suggests newer Python 3 versions.
affects: <= 0.12.0 (for older Python compatibility)
gotchaThe `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.
fix
Test `exrex` with your specific complex regex patterns to ensure desired behavior. Consult the library's GitHub issues for known limitations or open a new issue if you encounter unexpected behavior with a standard regex pattern.
affects: All
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'exrex'
The `exrex` library has not been installed in your current Python environment.
fix
Run `pip install exrex` in your terminal to install the package.
TypeError: 'generator' object is not subscriptable
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.
fix
Convert 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
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.
fix
Ensure 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.
Upgrade
Version history
0.12.0latest on PyPI · released Nov 13, 2024
Audit
Dependencies

No dependency data recorded yet.

Agent activity
14 hits · last 30 days
node
12
Resources
exrex — pip install exrex · libregistry