Write a regular expression explained step by step, with test cases

Get a regex for your language with every part explained, a table of cases that should and should not match, ready-to-use code and a warning about backtracking risks. Designed for ChatGPT, Claude, Gemini and Microsoft Copilot. It includes 5 customizable variables and you can copy it for free in one click.

Beginner · Code · Pending editorial review

Prompt

Act as a developer who is an expert in regular expressions and explains them clearly to learners.

I need a regular expression for: {{goal}}
- Language or engine: {{language}}
- Use: {{use}}
- Examples that MUST match: {{valid}}
- Examples that must NOT match: {{invalid}}

Deliverables:
1. The final regular expression, compatible with the given engine, with any flags it needs.
2. A piece-by-piece explanation in a table: fragment, what it means and why it is there. Cover anchors, groups (named if the engine supports them), quantifiers and character classes.
3. A readable, commented version (extended or `x` mode if the engine allows it, or built from parts if not).
4. A test table with all my examples plus at least five edge cases you add (empty string, spaces, letter case, accented characters, extreme length), with the expected result.
5. Code in my language that uses the regex for the stated purpose (validate, search, extract or replace) and runs the test table.
6. Risks: likely false positives and negatives, Unicode issues, and the risk of catastrophic backtracking (ReDoS) if the regex runs on user-supplied text.
7. When a regex is the wrong tool for this case and what to use instead (for example a parser or a validation library).

Constraints:
- If my examples are contradictory or not enough, tell me and ask before giving the final version.
- Prefer a clear, correct regex over a very short one that is hard to maintain.
- Do not claim the regex validates something 100% (an email address, for instance) when the standard is more complex.

How to use it

  1. Describe in words what you want to find or validate.
  2. Give several real examples that should match and some that should not.
  3. Run the test code you get back and add any cases that fail in real use.

Usage example

Goal: US ZIP codes, 5 digits with optional +4 · Language: JavaScript · Use: validate a whole field · Valid: 90210, 10001-1234 · Invalid: 9021, 90210-12.

Sample output

1. Final regex

/^\d{5}(?:-\d{4})?$/ (no flags needed).

2. Explanation

FragmentMeaning
^ … $Anchor to the whole field
\d{5}Exactly five digits (ASCII only in JS without the u flag)
(?:-\d{4})?Optional non-capturing group: hyphen plus four digits (ZIP+4)

5. Code and test table

const ZIP = /^\d{5}(?:-\d{4})?$/;

const cases = [
  ['90210', true], ['10001-1234', true], ['02134', true],
  ['9021', false],      // too short
  ['90210-12', false],  // incomplete +4
  ['902101', false],    // six digits
  ['ABCDE', false],
  ['', false], [' 90210', false], ['90210 1234', false], ['90210-', false],
];

const isZip = (value) => ZIP.test(value);

for (const [value, expected] of cases) {
  console.assert(isZip(value) === expected, `failed: "${value}"`);
}

Keep ZIPs as strings, never numbers: 02134 would silently become 2134. The pattern has no nested quantifiers, so there is no ReDoS risk. A regex checks the format only; it cannot tell whether a ZIP code is actually assigned…

Key facts

Category
Programming & development · Automations
Output type
Code
Level
Beginner
Designed for
ChatGPT, Claude, Gemini and Microsoft Copilot
Verified in
Pending editorial verification
Variables
What to find or validate, Language or engine, Use, Valid examples and Invalid examples
Language
English
Price
Free, no sign-up
Updated
Nov 7, 2026

Frequently asked questions

What is catastrophic backtracking?

It happens when a regex with nested quantifiers tries an enormous number of combinations on certain inputs and hangs. If the regex processes user data, it can be abused to take a server down (ReDoS).

Does the same regex work in every language?

Not always. Engines differ on named groups, lookbehind, Unicode classes and flags. That is why the prompt asks for your language and adapts the syntax.

What is this prompt for?

Get a regex for your language with every part explained, a table of cases that should and should not match, ready-to-use code and a warning about backtracking risks.

Which AI tools does it work with?

It is designed for ChatGPT, Claude, Gemini and Microsoft Copilot. We have not verified it in these tools yet, so results may vary between them.

How do I customize the prompt?

Click “Customize” and fill in the variables (What to find or validate, Language or engine, Use, Valid examples and Invalid examples). The text updates instantly and you can then copy it in one click. Values are stored only in your browser.

Do I need experience to use it?

No. It is a beginner-level prompt: just fill in the details and paste it into your AI tool.

Is this prompt free?

Yes. Every HomeCasa Prompts prompt is free, with no sign-up or subscription. You can also use them in professional projects.

Can I trust the AI output?

Treat it as a high-quality draft: always check data, figures and important claims before publishing or making decisions.

Related prompts