Prompt Library coding intermediate

ChatGPT Prompts for Regex: Write, Explain, and Test Regular Expressions

ChatGPT prompts for writing and debugging regular expressions. Pattern generation, explanation, test cases, and performance considerations for any regex flavor.

Tested on: GPT-4oClaude 4Gemini 2.5

The Prompt

Act as a regex expert who writes clear, well-commented regular expressions for production use.
Write a regular expression for:
Task: {describe what the regex should match or extract}
Language/flavor: {JavaScript / Python / Go / Java / PCRE / .NET}
Examples of strings to match: {3-5 examples that should match}
Examples of strings NOT to match: {3-5 examples that should not match}
Edge cases to handle: {whitespace variations / international characters / empty strings / etc.}
Performance sensitivity: {no constraints / must be efficient (no catastrophic backtracking)}

Output:
1. The regex pattern (formatted clearly)
2. Step-by-step explanation (break down each component: what it matches and why)
3. The regex in code (complete usage example in the requested language — with capture groups if extracting)
4. Test suite (5 test cases: 3 that match, 2 that don't — with explanation of why)
5. Edge cases analysis (what inputs might cause unexpected behavior)
6. Alternative approaches (if there's a simpler or more readable alternative — trade-offs noted)

Constraints:
- Complex patterns must use named capture groups for readability
- Catastrophic backtracking analysis required for patterns with nested quantifiers
- Code example must show both matching and extracting captured groups
- If the pattern is over 40 characters, add inline comments using verbose mode (where supported)

Variables to fill in

  • {task} What the regex should match, validate, or extract
  • {language/flavor} JavaScript, Python, Go, Java, PCRE, or .NET
  • {match examples} 3-5 strings that SHOULD match
  • {non-match examples} 3-5 strings that should NOT match
  • {edge cases} Specific edge cases to handle

How to use this prompt

  1. Provide concrete match and non-match examples — they're more informative than the text description
  2. Always specify the language/flavor — regex syntax differs meaningfully between JavaScript and Python
  3. Use the test suite to validate the pattern in your IDE before deploying
  4. Check the edge cases section before using on user input — that's where security issues hide
Code editor with regular expression pattern highlighted and test results
Photo by Markus Spiske on Unsplash

Examples beat descriptions for regex generation

Describing what you want to match in prose (‘phone numbers with or without country codes’) is ambiguous. Providing 5 concrete examples that should match and 5 that shouldn’t is unambiguous — the AI extracts the pattern from the examples directly. The prompt requires both categories before generating any output.

Catastrophic backtracking is a denial-of-service vulnerability

A poorly written regex with nested quantifiers can take exponential time on certain inputs — effectively hanging or crashing your application. Patterns like (a+)+ applied to a long string of as followed by something that doesn’t match can take seconds per character. The prompt’s performance analysis flags these patterns before you deploy them.

Named capture groups are documentation

A regex with capture groups indexed as $1, $2, $3 is hard to maintain. Named groups — (?P<year>\d{4}) in Python — make the pattern self-documenting. The prompt enforces named groups for any pattern over a certain complexity, which makes extraction code readable without regex reference in hand.