re | Python Standard Library – Real Python
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:
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 and backreferences
Frequently Used Classes and Functions
| Object | Type | Description |
|---|---|---|
re.compile() |
Function | Compiles a regular expression pattern for reuse |
re.match() |
Function | Determines if the regex matches at the start of a string |
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 |
Examples
Compile a regular expression pattern for reuse:
Search for a pattern match anywhere in the string:
Replace matches with a specified string:
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:
In this example, the re module efficiently extracts email addresses from the provided text, showcasing its power in text processing tasks.
Tutorial
Regular Expressions: Regexes in Python (Part 1)
In previous tutorials in this series, you've seen several different ways to compare string values with direct character-by-character comparison. In this tutorial, you'll learn how to perform more complex string pattern matching using regular expressions, or regexes, in Python.
For additional information on related topics, take a look at the following resources: