Regex with AI: Cheatsheet for Prompting AI to Write Patterns

Get perfect regex from ChatGPT, Claude, and Copilot every time

Ad placeholder (leaderboard)

Why AI is good at regex (and where it slips)

Regular expressions are one of the tasks large language models handle best, because the work is translating a precise intent into a compact, well-documented syntax — exactly the kind of pattern-mapping models excel at. Describe what you want in plain English and a capable assistant will hand you a working pattern in seconds, often with an explanation attached. The slip is that regex has many dialects and many silent failure modes: a pattern that looks correct can miss an edge case, behave differently across languages, or hang on hostile input. The fix is not to avoid AI but to prompt it precisely and test what it returns.

The anatomy of a good regex prompt

A reliable regex prompt has four parts. State the flavour — “JavaScript regex”, “Python re”, “PCRE”, “Go RE2” — because lookbehind, named groups, and Unicode handling differ between them. Describe the goal in one plain sentence. Provide positive and negative examples: a few strings that must match and a few that must not, which gives the model something concrete to satisfy instead of guessing. Finally, state your constraints — case sensitivity, anchoring to the whole string, allowed characters. A prompt with all four parts produces a correct pattern far more often than a vague “regex for email”.

Copy-ready prompt templates

For a new pattern: “Write a [language] regular expression that matches [description]. It must match these examples: [list]. It must not match these: [list]. Explain each part of the pattern and note any edge cases it does not handle.” For explaining an existing pattern: “Explain this [language] regex token by token in plain English, then list three inputs that would break it: [pattern].” For debugging: “This [language] regex should [goal] but fails on this input: [input]. Show the corrected pattern and explain what was wrong.” Keep these on hand and fill in the brackets — the structure is what makes the answers reliable.

Common patterns to ask for

The everyday patterns worth requesting by name are email addresses (and be explicit that fully RFC-compliant email regex is impractical — ask for a pragmatic version), URLs with optional protocol and query string, ISO and regional date formats, international and local phone numbers, IPv4 and IPv6 addresses, hex colours, slugs, and password-strength checks. For each, give the AI two or three real examples from your own data, because “phone number” means something different in every country and the model will otherwise pick a default that may not match your inputs.

Always test and check for backtracking

Never ship an AI-generated pattern on trust. Build a small test set of should-match and should-not-match strings and run the pattern against all of them — this catches the quiet failures that reading the regex never will. For any pattern applied to user-controlled input, ask the assistant explicitly: “Does this regex risk catastrophic backtracking, and if so, rewrite it to be linear-time.” Nested quantifiers and overlapping alternations can make a regex hang for seconds on a crafted string, which is a real denial-of-service vector. Used with specific prompts and disciplined testing, AI turns regex from a chore into a two-minute task — but the testing is the part you cannot delegate.

Ad placeholder (rectangle)