Back to the tool

Regex Named Capture Groups Explained

Regex named capture groups explained in one sentence: they let you write match.groups.year instead of match[1] and hoping you counted the parentheses right. If you have ever stared at a pattern with five groups and tried to remember whether the area code was group 2 or group 3, you already know why names exist.

The counting problem

Numbered groups work fine until the pattern grows. Take a phone number parser like /(\d{3})-(\d{3})-(\d{4})/. Group 1 is the area code, group 2 the exchange, group 3 the line. Easy. Now add an optional country code at the front. Every number after it shifts by one, and every line of code that reads match[2] now means something different. Nobody updates all of them. That is how a phone parser quietly starts returning the exchange where the area code should be.

Named groups fix this by decoupling the reading from the position. The name travels with the group no matter what you add, remove, or reorder around it.

Named capture groups explained with a real example

Parse a date, 2024-01-15, into year, month, and day. The named version in JavaScript:

const re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
const m = "2024-01-15".match(re);
m.groups.year;   // "2024"
m.groups.month;  // "01"
m.groups.day;    // "15"

The syntax is (?<name>...): a question mark, the name in angle brackets, then the pattern. Python spells it slightly differently, with a P after the question mark:

import re
m = re.match(r'(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})', "2024-01-15")
m.group("year")   # "2024"
m.groupdict()     # {'year': '2024', 'month': '01', 'day': '15'}

If you are skimming, the thing to remember is that JavaScript reads names off match.groups and Python reads them off m.group("name"), or the whole dict with m.groupdict().

When names pay off, and when they do not

My rule is simple: three or more groups, use names. One or two groups, numbers are fine and shorter. The payoff is not just readability. It is edit safety. Insert a group into a named pattern and nothing downstream breaks. Insert a group into a numbered pattern and you get to audit every index by hand, which is exactly the chore nobody does.

One more use worth knowing: backreferences. Inside the pattern you can match the same text a group captured with \k<name> in JavaScript, which makes duplicate-word detection read like English: \b(?<word>\w+)\s+\k<word>\b. Test any of this on regex101 with the flavor set to your language before it goes near production.

Frequently asked questions

What is the syntax for named capture groups?

JavaScript and most modern engines use (?<name>pattern). Python uses (?P<name>pattern) with a P after the question mark. Both capture the matched text under the given name.

How do I read a named group in JavaScript?

Through the groups object on the match: match.groups.year for a group named year. Numbered access like match[1] still works alongside it.

How do I read a named group in Python?

Pass the name to group(): m.group("year"). You can also get all of them at once with m.groupdict(), which returns a dictionary of name to matched text.

Do named capture groups work in every regex engine?

Most modern engines support them: JavaScript since ES2018, Python, .NET, Java, Ruby, and PCRE. Very old environments may not. Check your engine's docs if you target legacy runtimes.

Are named capture groups slower than numbered ones?

The difference is negligible in practice. Name your groups for readability; the regex engine does not care, but the next person reading your pattern will.

Related reading: Regex Lookahead and Lookbehind Explained: The Zero-Width Trick · Regex Greedy vs Lazy Matching: The One Question Mark That Changes Everything · Catastrophic Backtracking: The Regex That Took Down Cloudflare for 27 Minutes · Why Most Email Regex Patterns Are Wrong (And What to Use Instead)

Try the tool

Customize a regex template and get a plain-English explanation of every group, named or not. Build a regex template and stop counting parentheses.

Get new free tools by email

Want the next guide in your inbox? I publish one practical guide per new tool. Subscribe to the free newsletter on Substack. No spam, unsubscribe anytime.