Build and Explain a Regular Expression
Get a working regex with a plain-English breakdown and a set of passing/failing test cases.
This prompt gets you a working regular expression plus the thing most regex tools skip: a plain-English breakdown of every piece and a built-in test suite, so you can verify it does what you think before shipping it. It's built for the moment you know exactly what you want to match but don't want to reverse-engineer character classes and lookaheads by hand. Specifying the flavor matters because regex syntax differs across JavaScript, Python, and PCRE — the same pattern can behave differently or error out.
You are a regex expert. Write a regular expression that matches [DESCRIBE WHAT TO MATCH] in [LANGUAGE/FLAVOR: e.g. JavaScript / Python / PCRE]. Provide: (1) the regex pattern, (2) a line-by-line breakdown of each component in plain English, (3) 5 test strings — 3 that should match and 2 that should not. Wrap the pattern in a function that accepts a string and returns true/false.
What you can expect back
Pattern:
/^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$/
Breakdown:
- ^ start of string
- \(? optional opening parenthesis
- \d{3} three-digit area code
- \)? optional closing parenthesis
- [-.\s]? optional separator: dash, dot, or space
- \d{3} three-digit prefix
- [-.\s]? optional separator
- \d{4} four-digit line number
- $ end of string
Function:
function isUSPhone(input) {
return /^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$/.test(input);
}
Tests:
- '(555) 123-4567' -> true
- '555-123-4567' -> true
- '5551234567' -> true
- '55-123-4567' -> false (area code too short)
- '555-123-456' -> false (line number too short)Illustrative example — your results will vary by tool and inputs.
Get sharper results
- 01Always include the hardest edge cases in your description ('must reject numbers with letters, must allow an optional +1 country code') so the model designs for them instead of you discovering gaps later.
- 02Ask it to anchor with ^ and $ for full-string validation, or to drop them if you're searching for matches inside larger text — these serve opposite purposes.
- 03Have it add 'and explain the time complexity / any catastrophic backtracking risk' for patterns with nested quantifiers, since some regexes can hang on malicious input.
- 04Request the pattern in two flavors at once ('give me both the JavaScript and Python versions') when your validation runs on both front end and back end.
- 05Paste your real failing inputs back to the model and ask it to add them as test cases, then regenerate — this catches mistakes faster than eyeballing the pattern.
Adapt it for your case
Add 'use named capture groups so I can extract the area code and line number separately' to get a parser instead of a yes/no validator.
Replace the build request with 'Explain this regex line by line and give me 5 test strings' and paste a pattern you inherited.
Ask for two patterns — one strict and one permissive — plus a note on which to use for form validation versus log scraping.
Common questions
Why do I have to specify the flavor?
Features like lookbehind, named groups, and Unicode escapes differ between JavaScript, Python (re), and PCRE. A pattern that works in one can throw a syntax error or silently misbehave in another.
Should I trust the regex without checking?
Run the supplied test strings plus a few of your own real inputs first. The breakdown and tests exist precisely so you can verify rather than trust a black box.
What if my matching rule is really complex?
For deeply nested or context-sensitive rules, regex may be the wrong tool. Ask the model whether a small parser or a couple of simpler regexes would be more maintainable than one monster pattern.
You may also need
Generate a Production-Ready Dockerfile and Explain Each Line
Generate a hardened, multi-stage Dockerfile with line-by-line comments and matching .dockerignore.
Port a code snippet to another language idiomatically
Translates a snippet into idiomatic code in a target language and flags cross-language correctness pitfalls.
Diagnose and rewrite a slow SQL query using its plan
Reads a query plan to explain why a SQL statement is slow, then rewrites it and recommends indexes.
Debug an Error Message Step-by-Step
Get a structured debugging plan: error explanation, ranked root causes, tests, and fixes.