Regex Prompt: Testable Pattern, Not a Guess
Builds regex from test cases rather than guesses: strings that must match, strings that must not, and the explanation.
You are a regex specialist. Patterns are only correct relative to test cases, so we start there. TASK - What to match: [DESCRIPTION] - Flavour: [PCRE / JAVASCRIPT / PYTHON / JAVA / .NET] - Anchored or search anywhere: [FULL MATCH / ANYWHERE] MUST MATCH [LIST 5+ EXAMPLES] MUST NOT MATCH [LIST 5+ EXAMPLES] OUTPUT 1) PATTERN — with the delimiters and flags for my flavour. 2) EXPLANATION — part by part, in plain language. 3) TEST TABLE — every example above with match / no match and the captured groups. 4) FAILING CASES — inputs that would break this pattern (empty string, very long input, unicode, newlines). 5) SIMPLER ALTERNATIVE — if the pattern is unreadable, a simpler version and what it gives up. 6) WHEN NOT TO USE REGEX — if this problem should be solved by parsing instead, say so. RULES - Escape correctly for my flavour; note where the flavours differ. - Prefer readable over clever. No nested quantifiers that risk catastrophic backtracking. - If my test cases are contradictory, say which ones cannot both be satisfied.
If you cannot list five strings that must not match, the pattern is not specified yet.
02 — Variables
Fill in the brackets
The highlighted fields are the places where your own details go. You do not have to fill all of them; anything left empty is completed by the model's own assumption.
[DESCRIPTION]
[PCRE / JAVASCRIPT / PYTHON / JAVA / .NET]
[FULL MATCH / ANYWHERE]
[LIST 5+ EXAMPLES]
Field to fill in the prompt
03 — How to use
How to use it, step by step
- 01
Write the must-not-match list first — most regex bugs are over-matching.
- 02
State the flavour; the same pattern behaves differently in JavaScript and PCRE.
- 03
Add an empty string and a very long input to the failing cases; that is where backtracking blows up.
04 — Variations
Same prompt, different result
One prompt does not solve every case. These variations ask for the same job in a different tone, length or format — pick the one that fits.
Extract groups
Same pattern but with named capture groups for each field, and a table showing what each group contains for every example.
Replace
Same task as a search-and-replace: pattern, replacement string with group references, and the before/after for each example.
05 — Tips
Notes that take the result up a level
- A regex without a test table is a hypothesis. The table is the proof.
- Nested quantifiers on untrusted input are a denial-of-service risk, not just a style issue.
06 — FAQ
Frequently asked about Software and Code
Why is my regex slow?
Usually catastrophic backtracking from nested quantifiers like (a+)+. Make the quantifiers possessive or restructure the pattern.
Regex or parser?
Regex for flat, regular formats (codes, IDs, simple validation). A parser for anything nested (HTML, JSON, expressions).
Next
You may also find these useful
Code Writing Prompt: Clean, Testable Function
Defines a contract instead of a wish: language, version, input, output, error cases and tests.
Intermediate ChatGPTClaudeDebug Prompt: From "Why Doesn't It Work" to a Diagnosis
Converts "why doesn't it work" into a systematic diagnosis: reproduce, hypothesise, isolate, verify.
Intermediate ChatGPTClaudeRefactor Prompt: Improve Without Breaking Behaviour
Simplifies working code without changing behaviour, planned as reversible steps you can review one at a time.
Advanced ChatGPTClaudeSQL Query Prompt (Schema Included)
SQL written without the schema rarely runs; this prompt requires schema, relationships and expected output.
IntermediateThis page was prepared by the CopyPromt editorial team, first published 24 October 2026. Prompts are written to work across models; brand names are mentioned for identification only.