Skip to main content

Groups and Capturing

Basic Grouping with Parentheses

0:00
LearnStep 1/3

Creating Groups

Creating Groups and Alternation

In regular expressions, parentheses () serve two primary functions: grouping sub-patterns together and capturing the matched text for later use.

Creating Groups for Extraction

By placing parentheses around a part of your regex pattern, you create a "capture group". This allows you to isolate and extract specific segments of the matched string, which is essential for parsing structured text like logs, phone numbers, or emails.

python

Grouping for Alternation

The pipe symbol | represents a logical OR operation. Parentheses are crucial when you want to limit the scope of this alternation.

python