Regex from examples, with the failing cases

Builds a pattern from positive examples and then states what it will wrongly match.

Coding regexparsingvalidation Verified 2026-11-03

When to use it

You have a list of strings that should match and need to know what else will.

The prompt

Copy into your assistant
Write a regular expression that matches the following examples.

Then, before showing the pattern:
1. List the general form you inferred, including the edge cases the examples do not cover.
2. List at least three kinds of string that would match this pattern by accident.
3. State where the pattern is the wrong tool — nested structures, escaped quotes, anything not regular.

Only then output the pattern, with a one-line explanation per group.

Examples that should match:
<positive>

Examples that should not match:
<negative>

Flavour (PCRE, JS, Python):
<flavour>

What to change

Always supply negatives. Without them the model writes a pattern that matches your examples and half of everything else.

Why it works

Item 3 is the honest part: it tells you when you need a parser instead, which is the answer more often than people want to hear.

More coding prompts

Going deeper

More prompts

All prompts