Python Regex Capturing Groups – A Helpful Guide (+Video)

5/5 - (3 votes)

Python’s regex capturing groups allow you to extract parts of a string that match a pattern.

  • Enclose the desired pattern in parentheses () to create a capturing group.
  • Use to find matches, and access captured groups with the .group() method or by indexing the result.

For example: match ='(\d+)', 'abc123') captures the digits, and returns '123'.

One of the powerful aspects of Python’s regular expression capabilities is the use of capturing groups. By using capturing groups, you can easily extract specific portions of a matching string and efficiently process and manipulate data that meets a particular pattern.

Python Re Groups & Positive Lookahead [For Absolute Beginners]

I like to use capturing groups to isolate and extract relevant data from a given text. To define a capturing group, I simply place the desired regex rule within parentheses, like this: (rule). This helps me match portions of a string based on the rule and output the captured data for further processing.

πŸ’‘ Tip: An essential technique I employ while working with capturing groups is using the finditer() method, as it finds all the matches and returns an iterator yielding match objects that match the regex pattern. Subsequently, I can iterate through each match object and extract its value.

Before I’ll teach you everything about capturing groups, allow me to give some background information on Python regular expressions. If you’re already an expert, you can jump directly to the “capturing groups” part of the article.

Understanding Regular Expressions

As someone who works with Python, I often find myself using regular expressions.

πŸ‘©β€πŸ’» Recommended: Python Regex Superpower [Full Tutorial]

They provide a powerful tool for dealing with strings, patterns, and parsing text data. In this section, I’ll guide you through the basics of regular expressions and shed some light on capturing groups, which can be extremely helpful in many situations. 😊

Basic Syntax

Regular expressions, or regex, are patterns that represent varying sets of characters. In Python, we can use the re module to perform various operations with regular expressions. A key component of regex is the set of metacharacters, which help define specific patterns.

Some common metacharacters are:

  • . – matches any single character except a newline
  • \w – matches any word character (letters, digits, and underscores)
  • \d – matches any digit (0-9)
  • \s – matches any whitespace character (including spaces, tabs, and newlines)

It’s important to remember that these metacharacters must be preceded by a backslash to represent their special meanings.

Special Characters

There are several special characters in regex that have specific meanings:

  • * – matches zero or more occurrences of the preceding character
  • + – matches one or more occurrences of the preceding character
  • ? – matches zero or one occurrences of the preceding character
  • {n} – matches exactly n occurrences of the preceding character
  • {n,m} – matches a minimum of n and a maximum of m occurrences of the preceding character

These special characters can be combined with metacharacters and other characters to create complex patterns. My experience with Python’s regex capturing groups has been incredibly useful in extracting and manipulating specific parts of text data. Once you get the hang of it, you’ll find many ways to leverage these tools for your projects. πŸš€

Python Regex Module

In this section, I will share my knowledge on importing the regex module and some useful common functions when working with Python regex capturing groups. 😊

Importing the Module

Before I can use the regex module, I need to import it into my Python script. To do so, I simply add the following line of code at the beginning of my script:

import re

After importing the re module, you can start using regular expressions to perform various text searching and manipulation tasks. πŸš€

Common Functions

The Python regex module has several helpful functions that make working with regular expressions easier. Some of the most commonly used functions include:

  • re.compile(): Compiles a regular expression pattern into an object for later use. The pattern can then be applied to various texts using the object’s methods. Example:
pattern = re.compile(r'\d+')
  • Searches the given string for a match to the specified pattern. Returns a match object if a match is found, and None if no matches are found. Example:
result =, "Hello 123 World!")
  • re.findall(): Returns a list of all non-overlapping matches of the pattern in the target string. If no matches are found, an empty list is returned. Example:
result = re.findall(pattern, "My number is 555-1234, and my friend's number is 555-5678")
  • re.finditer(): Returns an iterator containing match objects for all non-overlapping matches in the target string. Example:
result = re.finditer(pattern, "I have 3 cats, 2 dogs, and 1 turtle")

By using these functions, I can effectively search and manipulate text data using regular expressions. Python regex capturing groups make it even simpler to extract specific pieces of information from the text. 🎯

Capturing Groups

As I dive into Python regex, one concept that has consistently come up is capturing groups. These groups simplify the process of isolating parts of a matched string for further use. In this section, I’ll discuss creating capturing groups, referencing captured groups, and the concept of non-capturing groups. Let’s dive in! 🌊

Creating Capturing Groups

Creating a capturing group is as simple as encasing a part of a regular expression pattern in parentheses. For instance, if I have the pattern (\d+)-(\d+), there are two capturing groups: one for each set of digits.

You can see this in action using the Python regex library like this:

import re

pattern = re.compile(r'(\d+)-(\d+)')
match ='Product: 123-456')

Now, the match object contains two captured groups πŸ†: one for '123' and another for '456'.

Referencing Captured Groups

After capturing groups, you might want to reference them for various operations. Using the group() method, you can obtain the values captured. You can access them by their index, where group(0) represents the entire matched string, and group(1), group(2), etc., correspond to the subsequent captured groups.

In my previous example, I can quickly access the captured groups like this:

first_group =  # '123'
second_group = # '456'

Pretty straightforward, right? πŸ˜„

Non-Capturing Groups

Sometimes, you want a group only for the regex pattern, without capturing its content. This can be achieved by using non-capturing groups. To create one, add ?: following the opening parenthesis: (?:...).

Here’s an example:

import re

pattern = re.compile(r'(?:ID: )(\d+)')
match ='User ID: 789')

In this case, the 'ID: ' portion is within a non-capturing group, and only the digits afterwards are captured. Now, if I reference the captured group, I only get the user ID:

user_id =  # '789'

And there you have it! I hope this illustrates the basics of Python regex capturing groups, including creating captures, referencing them, and when to use non-capturing groups. Happy regex-ing! πŸš€

Advanced Techniques

In this section, I will discuss some advanced techniques for working with capturing groups in Python regular expressions. These techniques, such as named capturing groups and conditional matching, can make your regex patterns more powerful and easier to read. Let’s dive in! 🌊

Named Capturing Groups

Named capturing groups allow you to assign a name to a specific capturing group. This makes your regex patterns more readable and easier to understand. In Python, you can define a named capturing group using the following syntax: (?P<name>...), where “name” is the desired name for the group, and “…” represents the pattern you want to capture.

For example, let’s say I want to extract dates with the format “MM/DD/YYYY“. Here’s how I can use named capturing groups:

import re

pattern = r"(?P&lt;month&gt;\d\d)/(?P&lt;day&gt;\d\d)/(?P&lt;year&gt;\d\d\d\d)"
date_string = "12/25/2020"
match =, date_string)

if match:

This will output:

Month: 12
Day: 25
Year: 2020

As you can see, using named capturing groups made our regex pattern more readable, and accessing the captured groups is much simpler. 😊

πŸ‘©β€πŸ’» Recommended: Named Capturing Groups Made Easy

Conditional Matching

Conditional matching in regex allows you to match different patterns based on the existence of specific capturing groups. In Python, you can use the following syntax for conditional matching: (?(id)yes|no), where “id” is the identifier for a capturing group, and “yes” and “no” are the patterns to match if the specified group exists, respectively.

For example, let’s say I want to find all occurrences of the word "color" or "colour" in a text. I can use conditional matching to achieve this:

import re

pattern = r"col(ou)?r(?(1)u|o)r"
text = "I like the color red. My favourite colour is blue."
matches = re.findall(pattern, text)

for match in matches:

This will output:


Here, we used conditional matching to identify both the American and British spellings of "color/colour" and print the captured group responsible for the difference. 🎨

I hope you find these advanced techniques useful in your Python regex adventures. Good luck exploring even more regex possibilities! 🐍

Practical Examples

In this section, I’ll demonstrate a couple of practical examples using Python regex capturing groups, 🐍🧩 focusing on email validation and URL parsing.

Email Validation

Validating email addresses is a common task in many applications. Using capturing groups, I can create a regex pattern to match and validate email addresses. Let’s get started. First, here’s the regex pattern:


In this pattern, I’ve used several capturing groups:

  • The first group ([a-zA-Z0-9._%+-]+) captures the username part of the email address. It includes letters, numbers, and some special characters.
  • The second group ([a-zA-Z0-9.-]+) captures the domain name, which consists of letters, numbers, and some special characters.
  • The third group ([a-zA-Z]{2,}) captures the top-level domain, consisting of at least two letters.

Now, let’s use this regex pattern in a Python function to validate an email address:

import re

def validate_email(email):
    pattern = r'^([a-zA-Z0-9._%+-]+)@([a-zA-Z0-9.-]+)\.([a-zA-Z]{2,})$'
    if re.match(pattern, email): # I match the input email against the pattern
        return True
        return False

URL Parsing

In this example, I’ll show you how to use capturing groups to parse and extract components from a URL. Let’s start with the regex pattern:


In this pattern, I’ve used several capturing groups:

  • The first group (https?) captures the protocol (http or https).
  • The second group ([^\s/:]+) captures the domain name.
  • The third group (:\d+)? captures the optional port number.
  • The fourth group (/)? captures the optional slash after the domain and port.
  • The fifth group (.*)? captures the remaining URL path, if any.

Now, let’s create a Python function to extract the components from a URL:

import re

def parse_url(url):
    pattern = r'^(https?)://([^\s/:]+)(:\d+)?(/)?(.*)?$'
    match = re.match(pattern, url) # I match the input URL against the pattern
    if match:
        return {
        return None

With this parse_url function, I can now extract and analyze various components of a URL. πŸŒπŸ”

Python Regex Course

Google engineers are regular expression masters. The Google search engine is a massive text-processing engine that extracts value from trillions of webpages.Β Β 

Facebook engineers are regular expression masters. Social networks like Facebook, WhatsApp, and Instagram connect humans via text messages.Β 

Amazon engineers are regular expression masters. Ecommerce giants ship products based on textual product descriptions.Β Β Regular expressions ​rule the game ​when text processing ​meets computer science.Β 

If you want to become a regular expression master too, check out the most comprehensive Python regex course on the planet: