AI Prompt to Generate a Regular Expression From a Plain-English Description
This regex generator prompt is for developers who know exactly what pattern they need to match but don't want to hand-write and debug regex syntax from scratch. Instead of staring at a reference sheet, you describe the matching rule in plain English — "match US phone numbers with optional area code parentheses" — and ask ChatGPT, Claude, or Gemini to produce the regular expression along with an explanation of each part.
The prompt below is built to get more than just a pattern back. It asks the model to explain what each group or symbol does, test the pattern against both valid and invalid example strings you provide, and flag any edge cases your plain-English description didn't account for, like Unicode characters, extensions, or multiple formatting variants. That last part matters: a regex that works on your three test cases but silently fails on a slightly different real-world input is a common source of production bugs.
Regex, by nature, is full of edge cases that are easy to describe loosely and hard to get exactly right, so before you drop a model-generated pattern into production code, running the filled-in prompt through Prompt Debugger is a useful extra step — it's built specifically to catch vague constraints and missed edge cases in a prompt before you rely on its output.
Prompt template
Want it sharper? Optimize this prompt with Prompt Optimizer, check it with the Prompt Debugger or shorten it with the Token Optimizer.
Example input
When to use it
- You know the matching rule conceptually but don't want to hand-write regex syntax from memory
- You're validating user input (emails, phone numbers, postal codes) and need a pattern that handles real-world format variations
- You're extracting specific substrings (IDs, dates, tags) from log files or text with a predictable but awkward structure
- You inherited a regex pattern from someone else's code and need a plain-English explanation of what it actually matches
Best practices
- Give the model at least 3-5 example strings that SHOULD match and 3-5 that should NOT match, not just a description
- Specify which regex flavor you need (JavaScript, Python, PCRE, POSIX) since syntax for things like lookaheads differs between them
- Ask explicitly for a plain-English breakdown of each capture group so you can maintain the pattern later without reverse-engineering it
- Test the generated pattern against your own real data, not just the examples you gave the model, since edge cases outside your examples are exactly where regex tends to break
Common mistakes
- Describing the rule too loosely ("match an email") instead of specifying the exact edge cases you care about (subdomains, plus-addressing, uppercase)
- Not telling the model which regex engine or language you're using, leading to syntax that doesn't work in your actual codebase
- Treating the model's explanation as proof the pattern is correct instead of actually running it against real test data
- Asking for one all-purpose pattern when the task really needs two simpler patterns combined in code
FAQs
Why does my regex work on some inputs but fail on others that look similar?
This almost always means your pattern was written to match only the examples you tested, not the full range of real-world variants. Giving the model both matching and non-matching examples up front, and asking it to call out uncovered edge cases, catches most of these gaps before the pattern ships.
Can ChatGPT or Claude write regex for any language or regex flavor?
Yes, but you need to specify which one (JavaScript, Python's re, PCRE, POSIX, etc.), since features like lookaheads, named groups, and escaping rules differ between flavors, and a pattern valid in one may throw a syntax error in another.
Is it safe to use an AI-generated regex in production without reviewing it?
No — always test it against your own real data, not just the examples in the prompt, since regex is notorious for passing a handful of test cases while silently failing on inputs nobody thought to check.
What tool can help me check this prompt's quality before using it?
Prompt Debugger — it scans a prompt for vague constraints and missed edge cases, which is exactly where regex-generation prompts tend to go wrong.