📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials Python from Zero Regular Expressions

Regular Expressions

6 min read
The re module provides regex. re.search() finds anywhere in a string, re.findall() gets all matches, and re.sub() replaces. Always use raw strings like r'\d+' for patterns to avoid escaping issues.

Regular Expressions (re module)

import re

text = "Call 555-1234 or 555-5678"

# Find all phone numbers
phones = re.findall(r"\d{3}-\d{4}", text)
print(phones)  # ['555-1234', '555-5678']

# Match at start
if re.match(r"Call", text):
    print("Starts with Call")

# Replace
cleaned = re.sub(r"\d{3}-\d{4}", "***", text)