Kavvo Prompts

Regex Writer Prompt

November 29, 2026 · 3 min read · By the Kavvo Prompts team

What this prompt does

This prompt writes a regular expression — a pattern for finding or checking text, such as email addresses or phone numbers — and explains each part of it in plain English. Regular expressions are powerful but famously hard to read, so the prompt asks the model to do three things: give you the pattern, explain what each piece does, and give you test cases that should match and test cases that should not.

The prompt

Act as a careful programmer who writes regular expressions and explains them clearly.

Write a regular expression for this job: [WHAT THE PATTERN SHOULD MATCH, e.g. "email addresses" or "UK postcodes"].

Programming language or tool: [LANGUAGE OR TOOL, e.g. "Python" or "JavaScript"]
Examples that SHOULD match:
- [EXAMPLE 1]
- [EXAMPLE 2]
- [EXAMPLE 3]
Examples that should NOT match:
- [EXAMPLE 1]
- [EXAMPLE 2]
Extra rules: [ANY EXTRA RULES, e.g. "the domain must end in .com or .co.uk"]

Give me:
1. The regular expression: in a code block, on its own, so I can copy it.
2. How to use it: a short code snippet in my language showing how to test a string against it.
3. Explanation: go through the pattern piece by piece and say what each part does, in plain English.
4. Test cases: a short list of strings that should match and strings that should not, based on my examples above.
5. Limits: 2-3 sentences on what this pattern does NOT handle, or where it could fail.

Rules:
- Keep the pattern as simple as it can be while still matching my examples.
- Do not invent a pattern for a different job than the one I described.
- If my examples contradict each other, say so instead of guessing.

Which model

Written for both ChatGPT and Claude. Paste it as one message. Both models write good regular expressions for common jobs. Claude often explains each part more clearly; ChatGPT often gives a more complete usage snippet.

How to use it

  1. Copy the prompt into ChatGPT or Claude as one message.
  2. Describe the job in one sentence: what should the pattern match? “Email addresses” is a job; “text” is not.
  3. Give three examples that should match and two that should not. The “should not” examples are what stop the pattern matching too much.
  4. Add your language or tool and any extra rules, then send it.
  5. Test the pattern on your own data, including the edge cases from your “should not” list, before you use it in real code.

Example

Input: Job: match UK postcodes. Language: Python. Should match: “SW1A 1AA”, “M1 1AE”, “EH1 1AA”. Should not match: “12345”, “SW1A 1AAA”.

Expected output: A regex in a code block, a short Python snippet using it, a piece-by-piece explanation (what each group of characters matches), a test list based on the examples given, and a limits note (for example, that the pattern checks the format, not whether the postcode is real).

Tips

    • Always give “should not match” examples. Without them, the model will write a pattern that matches far too much.
  • For email addresses, ask for a simple pattern, not a perfect one. A pattern that tries to match every valid email address becomes unreadable — and still misses cases.