Non-Capturing and Atomic Groups
In this step, we will explore how to fine-tune grouping behavior to optimize performance and control capturing.
Non-Capturing Groups (?:...)
Regular parentheses (...) create capturing groups, which store the matched text for later use. However, often you only need to group part of a regex to apply a quantifier or alternation without needing to retrieve the specific content. In these cases, non-capturing groups are preferred.
- Syntax:
(?:pattern) - Purpose: Groups tokens logically without assigning a group number or storing the match.
- Benefit: Improves performance and keeps group numbering clean.