grex
A command-line tool for generating regular expressions from user-provided test cases.
grex is a command-line tool that takes a set of example strings and
automatically generates a regular expression that matches all of them. Instead
of wrestling with regex syntax by hand, you simply provide the inputs you want
to match and let grex do the hard work.
Features
- Input-driven β provide example strings on the command line or via stdin, get a regex back
- Configurable output β control repetition, case insensitivity, anchoring, and more via flags
- Unicode-aware β handles multibyte characters and Unicode categories correctly
- Escape mode β optionally produce regexes that escape all non-ASCII characters
- Library too β usable as a Rust library (
grexcrate) in your own projects
Installation
cargo install grex
Or via your package manager:
# macOS
brew install grex
# Arch Linux
pacman -S grex
# Debian / Ubuntu
apt install grex
# Fedora
dnf install grexUsage
# Generate a regex from a set of example strings
grex "foo" "foobar" "foobaz"
# Output: ^foo(?:ba[rz])?$
# Case-insensitive matching
grex --ignore-case "Hello" "hello" "HELLO"
# Convert repetitions to quantifiers
grex --repetitions "aaa" "bbbb" "ccccc"
# Read test cases from stdin
echo -e "cat\ncar\ncap" | grex
# Escape non-ASCII characters
grex --escape "ΓΌber" "naΓ―ve"
# Anchor the expression to start and end of line
grex --anchor-start --anchor-end "abc" "abd"Why it's useful
Writing correct regular expressions is notoriously error-prone. grex is
invaluable when you have a known set of strings you want to match β log formats,
file name patterns, identifiers β and want to derive a reliable regex without
trial and error. It's also a great learning tool for understanding how regex
patterns map to real string examples.