Coding
AI Prompt for Testing Regex Patterns
Audit any regex with this AI prompt: piece-by-piece explanation, adversarial test cases, and a verdict on whether the pattern is safe to ship.
The prompt
Role: You are a regex auditor who stress-tests patterns before they reach production. Context: Regex flavor: [FLAVOR]. Where it is used: [USAGE_CONTEXT, e.g. email validation, log parsing, URL routing]. Pattern: [REGEX_PATTERN] Task: 1) Explain what the pattern matches in plain English. 2) Generate adversarial test cases: tricky inputs it SHOULD match, tricky inputs it should NOT match, and inputs designed to break it (empty string, unicode, extremely long input, newline tricks). 3) Run each test mentally and report pass/fail with reasoning. 4) Verdict: safe to ship, needs fixes (list them), or wrong tool for the job (suggest alternative). Format: Explanation first, then the test table (input | expected | actual | verdict), then the final verdict with fixes if needed. Ask for a catastrophic-backtracking stress test: a 10,000-character adversarial input and the expected match time. If the pattern hangs, request the possessive-quantifier or atomic-group rewrite for your flavor. Constraints: Be adversarial — your job is to break this pattern. Check ReDoS (catastrophic backtracking) risk explicitly with a long-input test.
Variables to fill in
Replace each [variable] in the prompt with your own details:
[FLAVOR] [USAGE_CONTEXT] [REGEX_PATTERN]
Example input
REGEX=/^[\w.-]+@[\w.-]+\.\w{2,}$/, FLAVOR=JavaScript, CONTEXT=email validation on signup form, QUESTION=what valid emails does this reject?
How to use this prompt
Say where the regex is used — validation, extraction, and routing need different strictness. Ask specifically what it wrongly accepts or rejects.
Tips for the best results
- Feed it your production data samples for the most realistic audit.
- Ask specifically about ReDoS — it is the regex vulnerability most developers miss.
- If the verdict is 'wrong tool', ask for the string-manipulation alternative.
Frequently asked questions
Will it admit when regex is the wrong tool?
It will tell you honestly — email regexes are the classic case where it recommends a simpler check plus verification email.
How do I check a regex for ReDoS risk?
Ask the AI explicitly — this prompt includes a long-input stress test. Nested quantifiers like (a+)+ are the classic warning sign it will flag.