Skip to main content

Groups and Capturing

Non-Capturing and Special Groups

0:00
LearnStep 1/3

Non-Capturing Groups

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.
python

Atomic Groups (?>...)

Atomic groups match a pattern and then discard any backtracking information for that group. This feature requires the third-party regex module, as Python's built-in re does not support it.