
A command-line tool for generating regular expressions from user-provided examples.
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, users can simply provide examples of strings they want to match, and grex will generate a regular expression guaranteed to match those specific inputs. The tool was originally inspired by the JavaScript regexgen project but has been rewritten in Rust with additional features and ongoing development.
The tool produces Perl-compatible regular expressions that work with Rust's regex crate and most other regex engines. It focuses on generating the most specific possible expressions by default, matching only the provided test cases and nothing else. However, grex offers numerous options to create more generalized patterns, including conversion to character classes (\d, \w, \s), detection of repeated substrings with quantifier notation, case-insensitive matching, and optional anchoring.
grex is particularly useful for developers who need to quickly create initial regex patterns from sample data, though the authors emphasize that understanding regular expressions is still essential. The generated expressions often serve as a starting point that can be manually optimized. The tool supports reading input from command-line arguments, files, or Unix pipelines, and includes features like syntax highlighting and verbose formatting for better readability.
While grex automates regex generation, it's designed to complement rather than replace regex knowledge. The tool excels at creating correct initial patterns from test cases, but users should still understand regex fundamentals to optimize and adapt the generated expressions for their specific use cases.
# via Homebrew
brew install grex
# via Cargo
cargo install grex
# via Binary download
Download from GitHub releases
