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 exrexVerified import paths — ran on the pinned version, not inferred.
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.
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.
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.
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.
Run `pip install exrex` in your terminal to install the package.
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)`).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.
No dependency data recorded yet.