Skip to content
Promptnic

Regex Builder With Test Cases

Builds a regular expression from plain English and proves it with matching and non-matching examples.

Open in:ChatGPTClaudeGrokPerplexity

The prompt

Write a regular expression for [REGEX_FLAVOUR] that matches: [WHAT_TO_MATCH] Must match: [EXAMPLES_THAT_MATCH] Must NOT match: [EXAMPLES_THAT_DONT] Return: 1. The regex. 2. A piece-by-piece explanation in a table. 3. A test table: each example, expected result, actual result. 4. Known false positives or negatives, and whether a parser would be safer than regex here.

Fill in the variables

The prompt above updates live. Nothing leaves your browser.

Remix it

One-click variations. Stack them, then copy.

Open in Prompt Lab →

Why this prompt works

Positive and negative examples act as few-shot specs, which pins down ambiguous requirements. The self-test table catches mistakes immediately, and asking whether a parser would be safer prevents regex for jobs regex should not do.

Framework: Few-Shot. Shows one or more examples of the output you want.

Example

Illustrative, not a real model output

Example input

[REGEX_FLAVOUR]
JavaScript
[WHAT_TO_MATCH]
Canadian postal codes
[EXAMPLES_THAT_MATCH]
T2P 1J9, t2p1j9
[EXAMPLES_THAT_DONT]
D2P 1J9, T2P-1J9X

What a good answer looks like (abridged)

/^[ABCEGHJ-NPRSTVXY]\d[ABCEGHJ-NPRSTV-Z] ?\d[ABCEGHJ-NPRSTV-Z]\d$/i | T2P 1J9 | match | match | | D2P 1J9 | no | no (D is never a first letter) |

How to use it

  1. Fill in 4 variables below (or in the prompt) with specific details.
  2. Paste it into ChatGPT (also works in Claude, Gemini, Grok).
  3. Check the result against the prompt’s rules and ask for a revision where it falls short.

ChatGPT tips

  • Put the output format (schema, table columns) near the top: ChatGPT follows explicit structure very reliably.
  • Use a Project or custom instructions for prompts you reuse daily.

Common mistakes

  • Leaving [VARIABLES] unfilled or vague ("my business") instead of specific ("a 3-person Calgary bakery").
  • Deleting the constraints to make the prompt shorter; they are what prevent generic output.
  • Accepting the first answer. Reply with what to fix and ask for a revision.

Guide: Promptnic template.

Frequently asked questions

Which AI model works best with the Regex Builder With Test Cases prompt?+

It is tuned for ChatGPT and also works in Claude, Gemini, Grok, DeepSeek. Put the output format (schema, table columns) near the top: ChatGPT follows explicit structure very reliably.

How do I use this prompt?+

Copy it, replace the 4 [VARIABLES] (regex flavour, what to match, examples that match…) with your details, then paste it into ChatGPT. You can fill the variables on this page and copy the finished version.

Is this prompt free to use?+

Yes. Every prompt on Promptnic is free to copy, adapt and use in personal or commercial work. A link back is appreciated, not required.

Why does this prompt work?+

Positive and negative examples act as few-shot specs, which pins down ambiguous requirements. The self-test table catches mistakes immediately, and asking whether a parser would be safer prevents regex for jobs regex should not do.

Related prompts