What this prompt does
This prompt turns the AI into a regex expert that builds a production-quality pattern for your [requirement] on a specific [regex_engine], then proves it works. You give it the requirement, the engine, a list of [valid_examples] that should match, and [invalid_examples] that should not, and it returns the pattern, a character-by-character plain-English breakdown, edge-case analysis, a performance and ReDoS check, ready-to-use [language] code, and a list of common mistakes the pattern avoids.
The structure works because it refuses to hand you a pattern you can't explain or trust. The plain-English section breaks down every token so a junior developer can follow it, the edge-case step tests empty strings, 1000+ character inputs, Unicode, whitespace-only strings, and your own [edge_case_concern], and the performance step checks for catastrophic backtracking and offers a ReDoS-safe variant if the primary pattern is vulnerable. Forcing named capture groups and a real explanation is how you avoid shipping a regex copied blindly from Stack Overflow.
When to use it
- You need a regex that guards real user input, not a throwaway script
- You want named capture groups for the meaningful parts of a match
- You are worried about catastrophic backtracking and need a ReDoS check
- You need the pattern to match your exact
[valid_examples]and reject[invalid_examples] - You want a plain-English explanation you can paste into a code review
- You need validation code plus unit tests in
[language], not just the raw pattern
Example output
You get the primary pattern (optimized for readability), an optional performance-tuned variant, and a character-by-character explanation table. Below that is an edge-case table showing match/no-match for tricky inputs, a backtracking and time-complexity analysis, ready-to-run [language] code with a validation function and unit tests covering all your examples, and three common mistakes this pattern sidesteps.
Pro tips
- Write
[requirement]precisely; "RFC 5322 email with subdomains and plus-addressing" beats "match emails" - Always set
[regex_engine]correctly, since PCRE2, JavaScript, and Python differ on lookbehind and Unicode - Give real
[valid_examples]and[invalid_examples]; the more representative they are, the tighter the pattern - Use
[edge_case_concern]to flag the weird input you actually fear, like quoted local parts in emails - Run the generated unit tests yourself; an AI claim that a pattern is ReDoS-safe still needs benchmarking
- If readability and performance variants differ, keep both and document which one is in production