re
The Python re module provides support for working with regular expressions, allowing you to search, match, and manipulate strings using complex pattern descriptions.
Regular expressions are a powerful tool for text processing and can be used for tasks such as validation, parsing, and string manipulation.
Here’s a quick example:
>>> import re
>>> pattern = r"\d+"
>>> re.findall(pattern, "There are 42 apples and 100 oranges")
['42', '100']
Key Features
- Supports Perl-style regular expressions
- Provides functions for searching, matching, and replacing text
- Allows compilation of regular expressions for reuse
- Supports advanced pattern features like lookaheads, backreferences, atomic grouping, and possessive quantifiers
Frequently Used Classes and Functions
| Object | Type | Description |
|---|---|---|
re.compile() |
Function | Compiles a regular expression pattern for reuse |
re.prefixmatch() |
Function | Determines if the regex matches at the start of a string, and is the Python 3.15+ name for re.match() |
re.search() |
Function | Searches for a pattern match anywhere in the string |
re.findall() |
Function | Finds all non-overlapping matches in a string |
re.sub() |
Function | Replaces matches with a specified string |
re.Match |
Class | Contains match details and results |
Select match(), search(), findall(), or sub() to run the same r"\d+" pattern on one editable string and compare which matches each function uses and what it returns:
Green highlights mark the matches the call uses, and dashed outlines mark matches it skips. With the default text, match() returns None because the string doesn’t start with a digit, while search() returns a re.Match for 42 only. Edit text so it starts with a number to see match() succeed, then compare the list from findall() with the str from sub().
Examples
Compile a regular expression pattern for reuse:
>>> pattern = re.compile(r"\b\w+\b")
>>> pattern.findall("This is a test.")
['This', 'is', 'a', 'test']
Search for a pattern match anywhere in the string:
>>> match = re.search(r"\d+", "There are 42 apples")
>>> match.group()
'42'
Replace matches with a specified string:
>>> re.sub(r"\d+", "#", "Room 123, Floor 4")
'Room #, Floor #'
Common Use Cases
- Validating input data, such as email addresses, phone numbers, and other similar data
- Parsing and extracting information from text files
- Replacing or reformatting strings based on patterns
- Tokenizing text for natural language processing
Real-World Example
Suppose you want to extract all email addresses from a block of text. You can use the re module to accomplish this:
>>> text = "Contact us at support@example.com or sales@example.org"
>>> email_pattern = r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}"
>>> emails = re.findall(email_pattern, text)
>>> emails
['support@example.com', 'sales@example.org']
In this example, the re module efficiently extracts email addresses from the provided text, showcasing its power in text processing tasks.
Related Resources
Tutorial
Regular Expressions: Regexes in Python (Part 1)
Learn regex matching in Python with re.search(), and use metacharacters, groups, lookarounds, and flags to find complex patterns in strings.
For additional information on related topics, take a look at the following resources:
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Oct. 25, 2026