Coding & Automation prompt

AI Prompt to Write and Explain a Regex (With Test Cases)

ChatGPTClaudeGeminiCursor
The short answer

Gets you a regular expression that matches what you want, rejects what you do not, and comes with a plain-English breakdown and tests. Copy the prompt below, replace the 5 [BRACKETED] variables with your own, and paste it into ChatGPT, Claude, Gemini or Cursor.

The prompt
You are a regular expression expert who writes patterns that are correct, readable and safe from catastrophic backtracking.

CONTEXT
- Regex flavor / language: [REGEX_FLAVOR] (for example JavaScript, Python re, PCRE, Go RE2, .NET, grep -E)
- What I want to match, in plain English: [MATCH_DESCRIPTION]
- Example strings that SHOULD match:
[SHOULD_MATCH_EXAMPLES]
- Example strings that should NOT match:
[SHOULD_NOT_MATCH_EXAMPLES]
- What I will do with it (validate whole input, find within text, extract groups, replace): [USE_CASE]

TASK
1. Note any ambiguity in my description and the assumption you are making.
2. Write the regex.
3. Break it down token by token in plain English.
4. Test it against every example I gave and show a table: string / expected / actual / pass or fail. If any fail, revise the pattern and re-test before answering.
5. Add 5 tricky edge-case strings I did not think of and say how the pattern handles each.
6. Show a short code snippet using it in my language, including the correct flags and anchors.

RULES
- Use only features supported by my flavor (for example no lookbehind where it is unsupported).
- Anchor the pattern when validating whole input.
- Avoid nested quantifiers and other constructs that risk catastrophic backtracking; say so if the task inherently risks it.
- Prefer clarity over cleverness. Use named groups when extracting values, if the flavor supports them.
- If a regex is the wrong tool (for example parsing HTML or full email RFC validation), say so and suggest the better approach.

OUTPUT FORMAT
Assumptions / Regex / Breakdown / Test table / Edge cases / Code snippet.

What to fill in

Everything in [GOLD CAPS] is yours to replace. Leave one blank and the machine will guess — and the machine guesses like a meatbag on a Monday.

  • [REGEX_FLAVOR]
  • [MATCH_DESCRIPTION]
  • [SHOULD_MATCH_EXAMPLES]
  • [SHOULD_NOT_MATCH_EXAMPLES]
  • [USE_CASE]

How to use it

  1. The should-NOT-match examples matter more than the should-match ones. Any regex can match your examples; few can reject the garbage.
  2. Paste the final pattern into a regex tester with your real data before shipping. The model tests in its head, and its head has been wrong before.
  3. If it tells you a regex is the wrong tool, believe it. Humans who parse HTML with regex become cautionary tales.

Example

REGEX_FLAVOR: Python re
MATCH_DESCRIPTION: UK postcodes like SW1A 1AA, with or without the space, case-insensitive
SHOULD_NOT_MATCH_EXAMPLES: SW1A1AAA, 12345, SW1A-1AA
USE_CASE: validate a form field

Works with

Tested for ChatGPT, Claude, Gemini, Cursor. Any modern chat model that follows long instructions will cope; the weaker ones will need you to restate the output format if they drift.

Weapons from the Arsenal that pair well with this prompt:

  • ChatGPT — OpenAI's general-purpose assistant: writing, research, images, voice and agents in one chat box.
  • Claude — Anthropic's assistant: the strongest long-form writer and careful reasoner in the consumer AI pack.