ConfigsApr 14, 2026·3 min read

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.

TL;DR
grex takes a list of example strings and automatically generates a regular expression that matches all of them.
§01

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.

§02

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.

§03

How to use

  1. Install grex: brew install grex (macOS), cargo install grex (from source), or download a binary.
  2. Run grex with your example strings as arguments.
  3. grex outputs a regex that matches all provided examples.
§04

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'
§05

Related on TokRepo

§06

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

What regex flavor does grex output?+

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.

Can grex handle Unicode strings?+

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.

Is grex available as a library?+

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.

How many examples should I provide?+

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.

Can grex generate regex for negative examples?+

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)

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets