
grex
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.
Installation
# via Homebrew
brew install grex
# via Cargo
cargo install grex
# via Scoop
scoop install grex

Alternatives & related tools
tailspin
Log file highlighter (binary: tspin) that colors dates, numbers, IPs, UUIDs, URLs and more in any log format, with no config, and pages output through less.
sd
Find and replace CLI tool with regex support, string-literal mode, and faster performance than sed
watchexec
File watcher that runs commands when files change, with .gitignore support and cross-platform filesystem monitoring
zizmor
Static analysis tool that finds and fixes security issues in CI/CD configurations, including GitHub Actions workflows, Dependabot, and pre-commit.
tealdeer
Fast Rust implementation of tldr providing simplified, example-based manual pages for command-line tools
add a badge to your README
Maintain grex? Show that it's featured on trmn. The badge links back to this page.
[](https://www.trmn.sh/tools/grex?utm_source=badge)<a href="https://www.trmn.sh/tools/grex?utm_source=badge"><img src="https://www.trmn.sh/badge/grex.svg" alt="Featured on trmn" height="20"></a>