PromptDev

Write a regex from a description

Describe what to match, list examples of both kinds, and get a pattern with a piece-by-piece explanation — plus its honest limits.

Last updated

Fill in the blanks

Assembled in your browser — nothing you type is stored or sent anywhere.

Your prompt

Write a regular expression (JavaScript) that matches: order IDs like ORD-2026-00042: the prefix ORD, a year, then a 5-digit number. It MUST match all of these: ORD-2026-00042 ORD-1999-00001 It must NOT match any of these: ORD-26-00042 ord-2026-00042 ORD-2026-42 Give the pattern, then explain it piece by piece, then list what it deliberately does not handle. Verify it against every example above before answering — if the requirements conflict, or an example can't be satisfied, say which and why instead of handing me a pattern that fails them. Prefer a readable pattern over a maximally clever one.

Model-agnostic — works in Claude, ChatGPT, Gemini or any AI chat.

Why this prompt works

  • →Positive AND negative examples turn a vague description into a testable spec — most bad generated regex comes from prompts that only said what should match.
  • →"Verify before answering" makes the model run its own pattern against your examples, which catches the majority of first-draft errors.
  • →Asking for deliberate limits up front beats discovering them in production — every practical regex has them.

Part by part

It MUST match all of these
Positive examples as a specification. A description alone is ambiguous; examples pin down what was actually meant.
It must NOT match any of these
The negative set, which is what stops an overly permissive pattern passing. Most bad regexes match everything they should and much more.
Verify it against every example above before answering
Asks for the check to happen before the answer. Models produce plausible patterns that fail their own examples, and this catches a good share of it.
if the requirements conflict
Some example sets cannot be satisfied together, and saying so is far more useful than a pattern that quietly fails two of them.
Prefer a readable pattern over a maximally clever one
Optimises for the person maintaining it. A dense regex nobody can modify is a liability regardless of correctness.

Common mistakes

  • Write a regex to match email addresses.

    Produces one of several conventional patterns with no way to know which edge cases it handles. Without examples you cannot tell whether it does what you need.

  • Supplying only positive examples.

    The easiest pattern satisfying any set of positives is one matching almost anything. Negative examples are what constrain it, and they are the half people leave out.

  • Write the most robust regex possible for this.

    Robust is read as covering every theoretical case, producing something unreadable and slow. Most tasks want a pattern handling your data, with the limits written down.

Questions

Why do negative examples matter more?

Because matching is easy and not-matching is the constraint. Any pattern can be loosened until the positives pass; the negatives are what force it to stay specific.

Should I test the result myself?

Always. Verification improves reliability and does not guarantee it, and a regex is exactly the kind of thing that looks right and fails on the third example. The regex tester on this site takes seconds.

Why does the dialect matter?

Because lookbehind, named groups and unicode properties vary between engines. A pattern written for one and pasted into another fails in ways that read as a bug in your input rather than in the pattern.

What if the pattern gets unmanageably long?

That is usually the signal to stop. Some problems — nested structures, context-dependent matching — are not regular, and a parser or a simple split-and-check is both shorter and correct.

Related in Prompts