Capturing groups: (...)
Wrapping part of a pattern in parentheses both groups it (for applying a quantifier or alternation) and captures whatever it matches, making that substring available afterward — via $1, \1, or a match array, depending on the language.
Pattern: (\d{3})-(\d{4})
Input: 555-1234
Group 1: 555
Group 2: 1234
Non-capturing groups: (?:...)
Sometimes you need grouping purely for structure — applying a quantifier to a whole sub-pattern, or building an alternation — without needing the matched text afterward. (?:...) groups without capturing, which avoids cluttering match results with groups you never intend to use:
Pattern: (?:https?|ftp)://\S+
Here the protocol alternation needs grouping to apply correctly, but there's no reason to capture "http" or "ftp" separately if the code afterward only cares about the whole matched URL.
Backreferences: reusing an earlier group within the same pattern
A backreference (\1, \2, etc.) refers back to whatever a numbered group actually matched, not the pattern that defined it — letting you require the same text to repeat elsewhere in the match:
Pattern: \b(\w+)\s+\1\b
Input: "hello hello" → matches (group 1 = "hello", repeated exactly)
Input: "hello world" → no match (second word doesn't equal group 1's captured text)
Input: "the theory" → no match
The \b word boundaries matter. Without them, (\w+)\s\1 finds "the the" inside "the theory", because \1 only has to match the start of the next word.
The same idea matches repeated delimiters that must agree, such as a quote with its own closing quote: (["'])(.*?)\1. It is also the classic way to pair an HTML tag with its closing tag, <(\w+)>.*?</\1>, though that only works for simple, non-nested markup; use an HTML parser for real pages.
Named groups: capturing groups with a label instead of a number
A named group is referenced by name instead of position, which becomes valuable once a pattern has several groups and numeric references ($3 vs $4) get hard to track:
Pattern: (?<area>\d{3})-(?<number>\d{4})
Referencing area and number directly is more maintainable than remembering which numbered position each represents, especially in a pattern that changes over time. The syntax differs between languages, and Python is the odd one out:
| Language | Define | Backreference in the pattern | Read the result |
|---|---|---|---|
| JavaScript | (?<area>...) |
\k<area> |
match.groups.area |
Python re |
(?P<area>...) |
(?P=area) |
m.group('area') |
| Java, .NET, PCRE (PHP) | (?<area>...) |
\k<area> |
m.group("area"), m.Groups["area"], $m['area'] |
Python's built-in re module rejects (?<area>...) with "unknown extension"; only the third-party regex package accepts both forms.
Common mistakes
- Using capturing groups everywhere out of habit, even when the captured value is never used — non-capturing groups keep match results clean and can be marginally more efficient.
- Confusing a backreference with repeating the same sub-pattern twice.
\b(\w+)\s+\1\brequires the same matched text to repeat;\w+\s\w+would match any two words, not necessarily identical ones. - Miscounting group numbers in a complex pattern with nested groups. Numbering follows the order of opening parentheses, left to right — nested and non-capturing groups can make manual counting error-prone; named groups avoid this entirely.
FAQ
What's the difference between (...) and (?:...)?(...) captures the matched text for later use; (?:...) groups for structural purposes (quantifiers, alternation) without capturing anything.
What does a backreference like \1 actually match?
Whatever text group 1 actually matched in this specific instance — not the pattern that defined group 1, the literal captured substring.
Are named groups supported in every regex engine?
In all the major ones, but not with the same syntax. JavaScript, Java, .NET and PCRE use (?<name>...); Python's re module needs (?P<name>...). POSIX tools such as grep -E and sed have no named groups at all.
Build and test patterns with capturing groups and backreferences live using the Regex Tester — runs entirely in your browser.
Use the Text Diff & Compare to compare your sample text before and after an edit. For a related walkthrough, read Regular Expressions: Patterns and Matches.