grex — A Command-Line Tool That Generates Regular Expressions from Examples
grex turns a list of example strings into a matching regex. Paste the patterns you want to match, get back a tested regex — no more staring at a regex cheat sheet for 30 minutes.
Ready-to-run agent install
This asset can be installed after the agent chooses its runtime, checks the plan, and runs the matching command.
npx -y tokrepo@latest install 8f0a62b4-3814-11f1-9bc6-00163e2b0d79 --target codexRun after dry-run confirms the install plan.
What it is
grex is a command-line tool written in Rust that generates regular expressions from user-provided example strings. Instead of manually crafting a regex pattern, you give grex a list of strings you want to match, and it produces the simplest regex that covers all examples.
It targets developers, data engineers, and anyone who writes regex infrequently enough that the syntax feels unfamiliar each time. Instead of consulting regex cheat sheets, you describe what you want by example.
How it saves time or tokens
Writing correct regex by hand requires recalling syntax for character classes, quantifiers, anchors, and lookaheads. Testing and debugging takes additional iterations. grex shortens this to a single command. Provide examples, get a working regex. The tool also supports options to produce more or less specific patterns, giving you control over generalization.
How to use
- Install grex:
brew install grex(macOS),cargo install grex(from source), or download a binary. - Run grex with your example strings as arguments.
- grex outputs a regex that matches all provided examples.
Example
# Generate regex for date patterns
grex '2026-01-15' '2025-12-31' '2024-06-01'
# Output: ^\d{4}-\d{2}-\d{2}$
# Generate regex for email-like patterns
grex 'user@example.com' 'admin@test.org' 'info@company.net'
# Output: ^[a-z]+@[a-z]+\.[a-z]{2,3}$
# Use flags for more specific patterns
grex --repetitions '192.168.1.1' '10.0.0.1' '172.16.0.1'
Related on TokRepo
- Automation tools — Tools that speed up repetitive developer tasks
- Coding tools — Development utilities and helpers
Common pitfalls
- grex generates a regex that matches all provided examples, but it may also match strings you did not intend. Provide enough diverse examples to constrain the pattern.
- The generated regex may be more general or more specific than desired. Use flags like --repetitions, --digits, and --words to control the level of generalization.
- grex works best for structural patterns (dates, IPs, codes). For semantic matching (e.g., 'all valid email addresses'), you still need hand-tuned regex or a dedicated parser.
Frequently Asked Questions
grex outputs Perl-compatible regular expressions (PCRE) by default. These work in most programming languages including Python, JavaScript, Java, Go, and Rust. Check your language's regex engine documentation for any flavor-specific differences.
Yes. grex supports Unicode character classes and can generate patterns that match non-ASCII text. Use the --non-ascii flag to enable Unicode-aware pattern generation for internationalized input.
Yes. grex is available as a Rust crate that you can integrate into your applications. It is also available as a WebAssembly build for use in browser-based tools and as a Python package.
The more diverse examples you provide, the better grex can generalize. A minimum of 3-5 examples that cover different variations (short/long, with/without special characters) produces the most useful patterns. Too few examples may generate an overly specific regex.
grex currently works with positive examples only. It generates a pattern that matches all provided strings. For excluding specific patterns, you would need to manually add negative lookaheads to the generated regex.
Citations (3)
- grex GitHub Repository— grex generates regex from example strings
- MDN Regular Expressions Guide— Regular expression syntax reference
- PCRE Documentation— Perl-compatible regular expression specification
Related on TokRepo
Discussion
Related Assets
fzf — Blazing Fast Command-Line Fuzzy Finder
fzf is a general-purpose command-line fuzzy finder written in Go. Blazing fast, portable, and composable with any list-producing command. Interactive picker for files, commands, history, git branches, processes, and more.
curl — The Command Line Tool for Transferring Data with URLs
curl is the most widely used command-line tool for transferring data with URL syntax. It supports HTTP, HTTPS, FTP, SFTP, and 25+ other protocols. Installed on billions of devices, curl and libcurl are foundational internet infrastructure.
jq — Lightweight Command-Line JSON Processor
jq is the essential command-line tool for processing JSON data. It lets you slice, filter, transform, and format JSON with a concise expression language — making it indispensable for working with APIs, config files, and data pipelines in the terminal.
Hyperfine — Command-Line Benchmarking Tool
Hyperfine is a command-line benchmarking tool written in Rust. Run benchmarks with statistical analysis: multiple runs, warmup, outlier detection, comparison across commands, and export results. By the author of bat and fd.