Prompts / Regex from examples

Coding regexparsing

Regex from examples

Build a regex from concrete match/non-match examples so the pattern is tested against your real edge cases, not just the obvious case.

Copying runs entirely in your browser - nothing here is ever sent anywhere.

Fill in the variables

I need a regular expression. Here's what it should match:

{{should_match}}

Here's what it should NOT match (these are the tricky edge cases - pay
close attention to these):

{{should_not_match}}

Context on where this will run (language/regex flavor, e.g. PCRE,
JavaScript, Python `re`): {{context}}

Give me the pattern, then walk through it piece by piece explaining what
each part does. Then explicitly check the pattern against every example
above (both the should-match and should-not-match lists) one by one, and
flag any example it doesn't handle correctly instead of silently leaving
it broken.

When to use

Anytime you’re tempted to write a regex from memory and hope it’s right - which is most of the time. Especially valuable for the edge cases (a trailing dot, an escaped character, an empty string) that are easy to forget when writing a pattern by hand.

Why it works

Regex mistakes are almost always in the edge cases, not the obvious case - a pattern that matches the “should match” example on the first try often still fails on a should-not-match case nobody thought to test. Giving both lists up front, and asking the model to explicitly check the pattern against every one of them, catches that instead of producing a pattern that merely looks right.

Variations

  • Ask for the pattern with named capture groups if you need to extract parts of the match, not just detect it.
  • Add “Also give me 3 more tricky edge cases I might have missed” before finalizing - self-generated adversarial examples often catch real gaps.
  • For a catastrophic-backtracking concern (a pattern that could hang on pathological input), ask explicitly whether the proposed pattern is safe against that, and why.