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.
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
DTM — Distributed Transaction Manager for Microservices
A cross-language distributed transaction framework supporting Saga, TCC, XA, and two-phase message patterns for reliable microservice coordination.
WatermelonDB — Reactive Database for React Native Apps
A high-performance reactive database framework for React Native and React web apps, built on top of SQLite with lazy loading and sync primitives.
Dexie.js — Minimalist IndexedDB Wrapper for the Web
A lightweight wrapper around IndexedDB that provides a clean Promise-based API for client-side storage in web applications.