Capturing Groups ()
Parentheses () create groups that serve two purposes:
- Group elements together for quantifiers
- Capture the matched text for later use
Basic Grouping
The (ha)+ treats "ha" as a unit, matching one or more repetitions.
Grouping for Quantifiers
Without groups, quantifiers only apply to the previous character:
ab+= a followed by one or more b's: "ab", "abb", "abbb"(ab)+= one or more "ab" sequences: "ab", "abab", "ababab"
Capturing Text
When you use groups, the regex engine captures what each group matched.
This captures:
- Group 1: "555" and "800"
- Group 2: "1234" and "9999"
Multiple Groups
Nested Groups
Groups can be nested:
Groups are numbered by opening parenthesis position:
- Group 1: "01/15" (outer group)
- Group 2: "01" (first inner)
- Group 3: "15" (second inner)
- Group 4: "2024" (last group)
Practical Examples
Practice Playground
Try capturing:
\((\d{3})\)- area code\$(\d+)\.(\d{2})- dollars and cents(\d{2}):(\d{2})- hours and minutes
Key Takeaways
- Parentheses
()group elements together - Groups let quantifiers apply to multiple characters
- The matched text in each group is captured
- Groups are numbered left-to-right by opening
( - Nesting creates sub-groups

