Example result: Write a regex from a plain description
AI-generated example · Regex Builder
Updated

Write a regex from a plain description

This prompt writes a regex from a plain description. You give the language or tool, examples that must match and examples that must not, and what you are trying to do. Test it on your own data.

text-regex-builder — chatgpt — 55 words

Write a regular expression.

Language or tool: [e.g. JavaScript, Python, grep]
It must match: [EXAMPLES THAT SHOULD MATCH]
It must not match: [EXAMPLES THAT SHOULD NOT]
Task: [FIND / VALIDATE / REPLACE]

Give the regex, a part-by-part explanation, a test table of my examples with expected results and any edge cases to watch out for.

Always test with your real data. Regex answers from AI can be subtly wrong.

Get a better result

What you will get

A regular expression for your language or tool, explained piece by piece, with a test table showing your matching and non matching examples and their expected results, plus edge cases to watch.

Why it works

Examples beat descriptions for regex, so the prompt asks for strings that must match and strings that must not. The part by part explanation lets you maintain it later. The test table turns your examples into a check, and the edge cases list points at the inputs most likely to slip past.

Common mistake

The regex matches part of a longer word.

Fix: Ask for word boundaries or start and end anchors where the whole value must match.

Also watch for
  • It works in one tool but not in yours. Name the exact tool, as the prompt asks, since features like lookbehind differ between them.
  • The test table says it passes but it does not. Run it on your real data, as the tip says, in your code or a regex tester, before you trust the table.
Try a variation: write it as code instead

Ask whether a few lines of normal code would be clearer than the regex for this task, and show both.

Still not right? Reply with this

This input should not match but it does: [EXAMPLE]. Fix the regex and add it to the test table.

More chat & writing prompts

See all 465 →