
Command-line tool that generates regular expressions from user-provided test cases
grex is a command-line utility and library that automatically generates regular expressions from user-provided test cases. Instead of manually crafting complex regex patterns, developers can input example strings they want to match, and grex produces a single regular expression guaranteed to match those inputs. The tool generates Perl-compatible regular expressions that are also compatible with Rust's regex crate.
The tool offers extensive configuration options including character class conversions (\d, \w, \s), case-insensitive matching, anchor removal, repetition detection with quantifier notation, and Unicode support. It can read test cases directly from command-line arguments, files, or Unix pipelines. Optional features include verbose formatting for readability and syntax highlighting in supported terminals.
grex is particularly useful for developers who need to create initial regex patterns quickly, though the generated expressions may require manual optimization for specific use cases. The tool uses an algorithm designed to produce the most specific regular expression possible by default, matching only the provided test cases and nothing else. It supports full Unicode Standard 16.0 compliance and correctly handles multi-symbol graphemes.
While grex automates regex generation, it's intended as a starting point rather than a replacement for regex knowledge. The generated patterns are often longer than hand-crafted alternatives, and understanding regex behavior remains essential for validating the results and optimizing for specific regex engines.
# via Homebrew
brew install grex
# via Cargo
cargo install grex
# via Scoop
scoop install grex
