LinuxCommandLibrary

exrex

Generate strings matching a regular expression

TLDR

Generate all possible strings that match a regex

$ exrex '[regex]'
copy

Generate a random string that matches a regex
$ exrex [[-r|--random]] '[regex]'
copy

Generate at most 100 strings that match a regex
$ exrex [[-m|--max-number]] [100] '[regex]'
copy

Generate all possible strings that match a regex, joined by a custom delimiter string
$ exrex [[-d|--delimiter]] "[, ]" '[regex]'
copy

Print count of all possible strings that match a regex
$ exrex [[-c|--count]] '[regex]'
copy

Simplify a regex
$ exrex [[-s|--simplify]] '[ab|ac]'
copy

Print eyes
$ exrex '[[oO0](_)[oO0]]'
copy

Print a boat
$ exrex '[( {20}(\| *\\|-{22}|\|)|\.={50}| ( ){0,5}\\\.| {12}~{39})]'
copy

SYNOPSIS


exrex [options] REGEX
exrex [options] -h | --help
exrex [options] -v | --version

PARAMETERS

REGEX
    The regular expression string from which to generate strings.

-a, --all
    Generate and print all possible unique strings matching the REGEX. This can produce a very large output for complex expressions.

-r, --random
    Generate and print a single random string matching the REGEX. This is the default behavior if no other output mode (like --all or --number) is specified.

-n N, --number N
    Generate and print N random strings matching the REGEX. This option implicitly enables random generation mode.

-m M, --max-repeat M
    Set the maximum number of repetitions for quantifiers like *, +, and {}. The default value is 5. This limits the complexity and length of generated strings.

-l L, --limit L
    Set the maximum number of generated strings. This is particularly useful when using --all to prevent excessive output and resource consumption.

-s SEED, --seed SEED
    Set the seed for the random number generator, ensuring reproducible results when generating random strings.

--encoding ENCODING
    Specify the character encoding for input and output, such as 'utf8' (default) or 'latin1'.

-h, --help
    Show the help message and exit.

-v, --version
    Show the program's version number and exit.

DESCRIPTION

exrex is a powerful command-line utility and Python library designed to generate strings that match a given regular expression. It can produce either all possible unique strings defined by the regex or a specified number of random strings. This tool is exceptionally useful for various tasks such as testing regular expressions, generating sample data for development or security analysis (e.g., fuzzing), and exploring the permutations a regex can define. It simplifies the process of creating valid input based on a pattern, saving significant manual effort and enabling rapid prototyping. The utility supports standard Python re module syntax, making it versatile for a wide range of common regex patterns, from simple character sets to complex group structures. Its primary aim is to automate the creation of data conforming to a defined pattern, proving indispensable in scenarios requiring patterned input validation or generation.

CAVEATS

Using exrex with the --all option on complex or broad regular expressions (e.g., those with many alternatives or unbounded repetitions) can lead to extremely long execution times and consume significant system memory, potentially exhausting resources. The --max-repeat option limits the depth of repetition, which can prevent the generation of all truly possible strings if the regex implies more repetitions than the set limit. As a Python-based utility, it requires a Python environment and installation via pip, and it adheres to Python's regular expression syntax.

INSTALLATION

exrex is not a built-in Linux command. It needs to be installed using Python's package installer, pip. To install it, run:
pip install exrex
Ensure you have Python and pip installed on your system.

REGEX SYNTAX COMPLIANCE

The regular expressions used with exrex must conform to Python's standard re module syntax. This includes support for most common regex features like character classes, quantifiers, groups, and anchors, but does not include advanced features specific to other regex engines (e.g., PCRE-specific lookarounds not supported by Python's re).

HISTORY

exrex emerged as a Python package designed to address the need for generating strings based on regular expressions, a capability not natively present in standard command-line tools. Its development as an open-source project provided a simple and effective command-line interface for Python's robust re module, making regex-based string generation accessible to a broader user base for various testing and data generation purposes.

SEE ALSO

grep(1), sed(1), awk(1), re (Python module)

Copied to clipboard