LinuxCommandLibrary

grex

Generate regular expressions from examples

TLDR

Generate a simple regex

$ grex [string1 string2 ...]
copy

Generate a case-insensitive regex
$ grex [[-i|--ignore-case]] [string1 string2 ...]
copy

Replace digits with \d
$ grex [[-d|--digits]] [string1 string2 ...]
copy

Replace Unicode word character with \w
$ grex [[-w|--words]] [string1 string2 ...]
copy

Replace spaces with \s
$ grex [[-s|--spaces]] [string1 string2 ...]
copy

Detect repeating patterns in the input and shorten them using {min,max} quantifiers
$ grex [[-r|--repetitions]] [string1 string2 ...]
copy

Generate regex of test cases (separated by newline) from a file
$ grex [[-f|--file]] [path/to/file]
copy

Do not generate anchors and non-capture groups
$ grex --no-anchors [[-g|--capture-groups]] [string1 string2 ...]
copy

SYNOPSIS

grex [OPTIONS] [INPUT]...

PARAMETERS

-C, --count-only
    Output only the counting regex (e.g., (?=(...)))

-d, --delimiter <DELIMITER>
    Delimiter for multiple inputs from stdin (default: \n)

-E, --escape-backslashes
    Escape backslashes in output regex

-e, --generate-escape
    Output escaped regex for shell use

-f, --from <FROM>
    Read patterns from file instead of args/stdin

-h, --help
    Print help

-i, --ignore-case
    Generate case-insensitive regex

-m, --min-chars <MIN_CHARS>
    Min chars for escape sequences (default: 4)

-M, --max-chars <MAX_CHARS>
    Max chars for escape sequences (default: 32)

-n, --no-capture
    Avoid capturing groups

-o, --only-chars <ONLY_CHARS>
    Escape only specified chars

-p, --perl
    Perl/PCRE compatible regex

-P, --pcrs
    PCRE2 compatible (implies -p)

-q, --quiet
    Suppress banner and stats

-r, --rust
    Rust regex crate compatible

-u, --unicode
    Enable Unicode (implies -i)

-V, --version
    Print version

DESCRIPTION

Grex is a command-line tool that automatically generates a compact, human-readable regular expression matching exactly the provided input strings.

Supply strings via arguments, stdin, or a file, and grex analyzes commonalities to produce an optimized regex using alternations, character classes, repetitions, and escapes. It supports multiple flavors: ECMAScript (default), Perl/PCRE, Rust regex.

Ideal for data validation, test fixtures, log patterns, or shell scripting where manual regex writing is tedious. Features include case-insensitivity, Unicode, customizable escapes, no-capture groups, and quiet mode. Output includes banner, stats, and the regex; use -q to suppress extras.

Grex prioritizes brevity: e.g., grex foo bar baz yields ba[rz]|foo. Handles duplicates, sorts inputs, and escapes safely.

CAVEATS

Regex matches exactly inputs; test thoroughly as optimizations may alter behavior. Performance drops with many/long strings. Not in core utils; requires installation.

EXAMPLE

grex foo bar baz
Outputs: ba[rz]|foo

echo 'a b c' | grex -d ' '
Outputs: a|b|c

INSTALLATION

Via Cargo: cargo install grex-cli
Arch: pacman -S grex
Fedora: dnf install grex
Others: check repos or binaries.

HISTORY

Created by Peter McCloghry-Vrenek (pmccloghryvi) in 2021 as Rust library/CLI. Inspired by regex generation needs; version 1.0+ on crates.io, ports to other langs.

SEE ALSO

grep(1), sed(1), awk(1), pcrepattern(3)

Copied to clipboard