Regular Expressions - search, findall, match & groups (Python Beginner Lesson #26)
Video: Regular Expressions - search, findall, match & groups (Python Beginner Lesson #26) by Taught by Celeste AI - AI Coding Coach
Watch full page →Regular Expressions in Python: search, findall, match & groups
Regular expressions (regex) are powerful tools for pattern matching and text processing in Python. This guide covers the essentials of the re module, including how to search, match, and extract patterns using functions like re.search(), re.match(), and re.findall(). You'll also learn about raw strings, metacharacters, quantifiers, capturing groups, and substitution to handle complex text tasks efficiently.
Code
import re
text = "Contact us at support@example.com or call 123-456-7890."
# Using raw strings (r"") to avoid escaping backslashes
email_pattern = r"[\w\.-]+@[\w\.-]+\.\w+"
phone_pattern = r"\d{3}-\d{3}-\d{4}"
# re.search() finds the first match anywhere in the string
email_match = re.search(email_pattern, text)
if email_match:
print("Email found:", email_match.group())
# re.match() checks if the pattern matches at the start of the string
start_match = re.match(email_pattern, text)
print("Match at start:", start_match) # None because email not at start
# re.findall() extracts all matches as a list
phones = re.findall(phone_pattern, text)
print("Phone numbers found:", phones)
# Using capturing groups to extract parts of a pattern
date_text = "Today's date is 2024-06-15."
date_pattern = r"(\d{4})-(\d{2})-(\d{2})"
date_match = re.search(date_pattern, date_text)
if date_match:
year, month, day = date_match.groups()
print(f"Year: {year}, Month: {month}, Day: {day}")
# Substitution with re.sub()
clean_text = re.sub(r"\d", "#", text) # Replace digits with '#'
print("Cleaned text:", clean_text)
Key Points
- The
remodule provides functions likesearch(),match(), andfindall()for flexible pattern matching. - Raw strings (prefix
r) are essential for writing regex patterns without doubling backslashes. - Metacharacters like
\d(digits),\w(word chars), and quantifiers control pattern matching precision. - Capturing groups
()allow extracting specific parts of matched text for further processing. re.sub()enables search-and-replace operations on strings using regex patterns.