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: (\w+)\s\1
Input: "hello hello" → matches (group 1 = "hello", repeated exactly)
Input: "hello world" → no match (second word doesn't equal group 1's captured text)
This is exactly how you'd match doubled words, matching HTML tags with their own closing tag (<(\w+)>.*</\1>), or repeated delimiters that must match each other rather than being independently defined twice in the pattern.
Named groups: capturing groups with a label instead of a number
Most modern regex engines support (?<name>...) — a capturing group 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.
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.
(\w+)\s\1requires 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?
Support is broad in modern engines (JavaScript, Python, PCRE, etc.) but syntax can vary slightly — check your specific regex flavor's documentation.
Build and test patterns with capturing groups and backreferences live using the Regex Tester — runs entirely in your browser.