🤖 The AI Prompt That Rewrites a Regex With Named Groups
A regular expression with a handful of unnamed capturing groups is easy to write once and genuinely hard to come back to six months later – every \1, \2, group(3) reference in the surrounding code is a number that means nothing without re-deriving the pattern by eye. Converting those groups to named captures doesn’t change what the pattern matches, but it does mean the surrounding code, and the next person reading it, can ask for “the year” or “the domain” instead of “group 4”.
📋 The Prompt
Rewrite the following regular expression so it is easier for a human to read and maintain, without changing what it matches: - Convert every capturing group into a named group using the host language's named-group syntax (tell me which language/regex flavor you're targeting if it's ambiguous: PCRE, .NET, Python re, JS, etc.) - Add a short comment above the pattern (or inline, if the flavor supports extended/verbose mode) explaining what each named group is meant to capture - Call out, separately, any part of the original pattern whose behavior you are NOT fully certain of (a greedy vs. lazy quantifier that looks accidental, an escaped character that may have been meant literally, a group that's captured but never referenced) rather than silently "fixing" it - Provide a handful of example input strings, with which group captures which substring, so the rewrite can be verified against the original pattern's actual behavior before it replaces it Original pattern (language: __): [paste regex]
✅ Use It Well
- Always specify the regex flavor (.NET, PCRE, Python, JavaScript, RE2, …) since named-group syntax differs – (?<name>…) versus (?P<name>…) – and a few flavors don’t support it at all.
- Ask for example strings and their capture results before swapping the rewritten pattern into production code, so the rewrite can be diffed against the original on real input rather than trusted on the model’s say-so that nothing changed.
- When the model flags a quantifier or escape it isn’t sure was intentional, treat that as a cue to check the regex’s git blame or original author rather than letting the rewrite “helpfully” resolve the ambiguity either way.
🚨 Why This Is Dangerous
A regex rewrite that looks cleaner and passes a handful of example strings can still subtly change behavior on an edge case nobody thought to test – a seemingly cosmetic change to a validation or parsing regex deserves the same test coverage as any other change to code that decides what counts as valid input.
A regex with named groups isn’t a different regex – it’s the same regex with its intent finally written down where the next person can find it.




