Lesson 22 of 30
Regular Expressions
Pattern matching, searching, replacing, and validating data with re module.
What are Regular Expressions?
Regular expressions (regex) are patterns used to match, search, and transform text. Python's re module provides regex support.
Basic Matching
import re
text = "My phone is 012-3456789"
match = re.search(r"\d{3}-\d{7}", text)
if match:
print(match.group()) # 012-3456789
Common Patterns
| Pattern | Matches |
|---|---|
| \d | Any digit 0–9 |
| \w | Word character (a–z, 0–9, _) |
| \s | Whitespace |
| . | Any character except newline |
| ^ | Start of string |
| $ | End of string |
| + | One or more |
| * | Zero or more |
| ? | Zero or one |
findall and sub
emails = re.findall(r"[\w.-]+@[\w.-]+\.\w+",
"Contact [email protected] or [email protected]")
print(emails) # ['[email protected]', '[email protected]']
cleaned = re.sub(r"\s+", " ", "too many spaces")
print(cleaned) # "too many spaces"
Validating an Email Address
def is_valid_email(email):
pattern = r"^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,}$"
return bool(re.match(pattern, email))
print(is_valid_email("[email protected]")) # True
print(is_valid_email("not-an-email")) # False
✅ Tip — Raw Strings
Always use raw strings (prefix r"...") for regex patterns. Without the r, backslashes like \d need to be doubled: \\d. Raw strings keep patterns readable.