
CLI tool that generates regular expressions from user-provided test cases automatically
grex is a command-line utility and Rust library that automatically generates regular expressions from user-provided test cases. Instead of manually crafting regex patterns, developers provide example strings and grex produces a Perl-compatible regular expression guaranteed to match those inputs. The tool accepts test cases either directly as command-line arguments (grex a b c) or from files, and supports Unix pipelines for input.
The tool generates the most specific regex possible by default, matching only the provided test cases and nothing else. It supports character classes, alternation with | operators, quantifiers like {min,max}, optional groups with ?, and detection of common prefixes and suffixes. Users can enable more generalized patterns through flags like --digits (converts to \d), --spaces (converts to \s), and --words (converts to \w).
grex includes several output options including verbose mode for more readable multi-line expressions, syntax highlighting in supported terminals, and case-insensitive matching. The generated expressions are fully compatible with Rust's regex crate and comply with Unicode Standard 16.0. While the tool provides a correct starting point for regex development, the authors emphasize that developers should still learn regular expression syntax for optimization and domain-specific adjustments.
The tool is particularly useful for developers who need to quickly prototype regex patterns from known examples, validate regex logic against test cases, or generate initial patterns that can be refined manually. It's available as both a standalone CLI tool and a Rust library for programmatic use.
# via Homebrew
brew install grex
# via Cargo
cargo install grex
# via Scoop
scoop install grex
