💻 Coding

Regex Pattern Writer with Line-by-Line Explainer

Write a tested regex for your language, explain every token, show match and miss cases, and warn about ReDoS and flags.

0.0
0Reviews
P
August 24, 2026

Prompt

Act as a language-aware regex engineer. Write a pattern that is correct for the stated engine, not a generic cheat-sheet. Prefer boring, linear patterns over clever ones. Call out ReDoS.

Inputs:
- Job: [What must match, what must fail]
- Language / engine: [JS / Python / PCRE / Go / Java / ripgrep]
- Examples that MUST match: [List]
- Examples that MUST fail: [List]
- Flags: [i m s u or none]
- Anchors: [full string / find in text / line]
- Capture groups I need: [Named groups or none]
- Unicode?: [Yes / No]
- Replace?: [If search-replace, the desired rewrite]

Generate:
1. Engine notes: two lines on lookbehind, named groups, digit classes, lastIndex.
2. Recommended pattern: copy-paste literal or raw string for that language. Flags. One-line contract.
3. Token table: every token or group, what it does, why it is there.
4. Capture map: group numbers and names, extract from the first MUST match.
5. Match table: each given MUST match and MUST fail, result, deciding token. If a MUST example fails, fix the pattern.
6. Extra probes (8): 4 should-match and 4 should-fail guesses the user did not list. Mark GUESS.
7. ReDoS / perf: linear or not, nested quantifiers, worst-case input, safer rewrite if needed.
8. Code snippet: compile and assert the given examples. Idiomatic for the engine.
9. Alternatives declined: 1-2 clever patterns and why you did not ship them.

Constraints:
- Do not use stacked greedy dots if a character class will do.
- Never paste a complete RFC email regex.
- If the job is underspecified, state one assumption.
- No fake regex101 scores.

Instructions

Replace every [bracket] with your details before running. Works on ChatGPT, Claude, and Gemini.

Examples

Example Input

Job: validate US SKUs like NW-1842-BLU (prefix 2-5 caps, 3-5 digits, 3-letter color). Fail missing hyphens, lowercase prefix, extra tail.
Language: Python re
MUST match: NW-1842-BLU, HATCH-100-RED, AB-99999-GRY
MUST fail: nw-1842-BLU, NW1842BLU, NW-1842-BLUE, NW-1842-BLU-X, empty string, NW-18-BLU
Flags: none (case-sensitive)
Anchors: full string
Capture groups: prefix, num, color
Unicode: No
Replace: none

Example Output

1. Engine notes
Python re: named groups (?P<name>...). Use [0-9] for ASCII digits. No possessive quantifiers. fullmatch is the right API for full-string.

2. Pattern
Job text said prefix 2-4, but HATCH-100-RED is a MUST match (5 letters). Examples win.

sku_re = re.compile(r"^(?P<prefix>[A-Z]{2,5})-(?P<num>[0-9]{3,5})-(?P<color>[A-Z]{3})$")
Flags: none. Contract: whole string. Prefix 2-5 A-Z, hyphen, 3-5 digits, hyphen, exactly 3 A-Z.

3. Token table
^ start. (?P<prefix>[A-Z]{2,5}) caps 2-5. literal hyphen. (?P<num>[0-9]{3,5}) digits 3-5. literal hyphen. (?P<color>[A-Z]{3}) three caps. $ end.

4. Capture map
NW-1842-BLU: prefix=NW, num=1842, color=BLU.

5. Match table
NW-1842-BLU match. HATCH-100-RED match (prefix 5). AB-99999-GRY match (num 5).
nw-1842-BLU fail: [A-Z] vs n.
NW1842BLU fail: hyphens required.
NW-1842-BLUE fail: color length 4.
NW-1842-BLU-X fail: tail after color.
empty fail.
NW-18-BLU fail: num length 2.

6. Extra probes (GUESS)
Should-match: ABCD-1234-RED; NW-100-BLU; XY-12345-GRY.
Should-fail: A-123-BLU (prefix 1); NW-1842-blu; leading space; NW--1842-BLU.

7. ReDoS
Linear. No nested quantifiers. Fine for SKU fields.

8. Code
import re
sku_re = re.compile(r"^(?P<prefix>[A-Z]{2,5})-(?P<num>[0-9]{3,5})-(?P<color>[A-Z]{3})$")
ok = ["NW-1842-BLU", "HATCH-100-RED", "AB-99999-GRY"]
bad = ["nw-1842-BLU", "NW1842BLU", "NW-1842-BLUE", "NW-1842-BLU-X", "", "NW-18-BLU"]
assert all(sku_re.fullmatch(s) for s in ok)
assert not any(sku_re.fullmatch(s) for s in bad)

9. Declined
^[A-Z]+-\\d+-[A-Z]+$ too loose (BLUE). Nested (A+|B+)+ is a ReDoS toy, useless here.
Spec clash: job said prefix 2-4; example HATCH is 5. Widened to 2-5. If catalog is really 2-4, drop HATCH from MUST.

Reviews (0)

Please login to leave a review.
Loading reviews...