The prompt
You are helping me build a regular expression. Work from my examples rather than from a general idea of the format. **1. Restate the requirement** Say what must match and what must not, based on my examples. If my examples are ambiguous or contradict each other, ask before writing a pattern. **2. The pattern** Give it for the flavour I named. Then give a commented version broken across lines, one construct per line, so each piece can be read on its own. **3. Trace it** Walk one matching example through the pattern token by token. **4. What breaks it** List concrete inputs that will be wrongly accepted or wrongly rejected. Be specific — Unicode, empty strings, leading and trailing whitespace, lengths at the boundary, and anything my examples did not cover. **5. Catastrophic backtracking** Say whether nested quantifiers could make this pattern run in exponential time, and if so give the input that triggers it and a rewrite that does not. **6. When not to use a regex** If the real answer is a parser, a library, or a built-in URL or email type, say so plainly rather than producing a 400-character pattern. --- Flavour: [JavaScript / PCRE / Python / Go RE2 / grep] Must match: [YOUR EXAMPLES] Must not match: [YOUR COUNTER-EXAMPLES]
Replace everything in [SQUARE BRACKETS] before you run it. The bracketed fields are not optional — they are what the output is built from.
Why this prompt is built this way
The instruction that makes this work is the first line: work from my examples rather than from a general idea of the format. Regular expressions built from a description — "match an email address" — come back as one of the famous monster patterns that is simultaneously too strict and too loose. Built from your actual data, the pattern matches your actual data.
Requiring counter-examples is the other half, and it is the step people skip. A pattern derived only from strings that should match has no pressure to exclude anything, so it ends up permissive in ways you discover in production. Two or three things that must not match constrain the solution far more than another ten that should.
Section 5 on catastrophic backtracking addresses a real denial-of-service risk rather than a theoretical one. Nested quantifiers such as (a+)+ can take exponential time on input that nearly matches, and a regex applied to user-supplied strings is an attack surface. Section 6 — say when not to use a regex at all — is there because the honest answer for URLs, emails and HTML is usually a parser or a built-in type, and a model will otherwise cheerfully produce the 400-character pattern you asked for.
What to use it for
- Log parsing
- Input validation
- Bulk find and replace
- Understanding an inherited pattern
Adapting it to your situation
- Always supply counter-examples. If you do only one thing from this list, do this one.
- Name the flavour precisely. Go RE2 has no backreferences or lookarounds at all; JavaScript lookbehind is newer than some runtimes. A pattern for the wrong flavour simply fails to compile.
- Include the ugly real inputs — the ones with trailing whitespace, the empty line, the row with a stray quote. Those are what the pattern will meet.
- Ask for a non-regex version too when parsing structured data. Often it is shorter and always it is more readable.
Where this goes wrong
Test the pattern against real data before shipping it, not just against the examples you supplied. The model optimised for exactly those strings, and regex failures are silent — a validator that is slightly too permissive lets bad data in without erroring.
Be especially careful applying a generated regex to user input in a request path. If section 5 flags backtracking risk, take it seriously rather than shipping and watching.
Which model to run it on
All three are capable. Whichever you use, the quality of output tracks the quality of your examples far more than it tracks the model.
Practical tips
Always supply counter-examples. A pattern built only from things that should match will be far too permissive, and you will not notice until production data arrives. Go RE2 has no backreferences or lookarounds — say so up front if that is your target.