Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
AI

The AI Prompt That Rewrites a Regex With Named Groups

- 28.11.26 - ErcanOPAK

🤖 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.

— AI Power User

Related posts:

AI Prompt: Generate Business Plan

AI Prompt: Generate Business Plan

The AI Prompt That Turns a Slow SQL Query and Its Execution Plan Into a Prioritized Fix List

The AI Prompt That Turns a Slow SQL Query and Its Execution Plan Into a Prioritized Fix List

The AI Prompt That Finds Every Place Your API Silently Changes Behavior Based on Header Casing Becau...

The AI Prompt That Finds Every Place Your API Silently Changes Behavior Based on Header Casing Becau...

Views: 7
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

Docker: Fix a Build Secret Leaking Into a Cached Layer
HTML5: Fix a Required Field Skipping Screen Reader Focus

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,079 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 997 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 996 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 868 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 671 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 631 views
  8. 08Average of all values in a column that are not zero in SQL 584 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 562 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com