Exrex

0.12.0 · active · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

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}")

view raw JSON →