Regular expressions (regex) are a powerful tool in programming, used for searching, manipulating, and editing text based on defined patterns. In Python, regular expressions are supported by the re
module, providing a robust set of functions to work with string data. This article delves into how to leverage Python’s regex capabilities for finding patterns within strings.
Understanding Python’s re
Module
Before diving into pattern matching, it’s essential to understand the basics of the re
module in Python. This module offers various functions like search()
, findall()
, match()
, etc., to perform different operations using regular expressions.
Key Functions in re
Module:
re.search()
: Returns a Match object if there is a match anywhere in the string.re.match()
: Returns a Match object if the pattern matches at the beginning of the string.re.findall()
: Returns a list of all non-overlapping matches in the string.re.finditer()
: Returns an iterator yielding Match objects over all non-overlapping matches.
Practical Example: Finding Email Addresses
Let’s illustrate the use of regular expressions in Python with a practical example. Suppose we need to find all email addresses in a given text. An email address typically follows the pattern username@domain.com
.
Python Code Example
import re
def find_emails(text):
# Regular expression for matching email addresses
email_pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
# Using re.findall() to extract all email addresses
emails = re.findall(email_pattern, text)
return emails
# Example text
text = "Contact us at support@example.com, sales@example.net, or info@example.org."
emails = find_emails(text)
print("Found emails:", emails)
Output
Found emails: ['support@example.com', 'sales@example.net', 'info@example.org']