- What are regex — pattern matching in Python
- re.search() — finding patterns in text
- re.findall() — finding all matches
- re.sub() — replacing patterns
- re.split() — splitting with patterns
- Groups and flags — advanced regex techniques
What Are Regular Expressions?
Regular expressions, often called "regex" or "regexp," are like a secret language for finding patterns in text. They let you search for specific patterns, extract information, and transform text in ways that would be incredibly tedious to do manually.
Think of regex like a super-powered search bar. Instead of just searching for exact words, you can search for patterns — like "any phone number," "any email address," or "any word that starts with 'p' and ends with 'n'." It's the tool professionals use for text processing.
In Python, you work with regular expressions using the re module. It gives you everything you need to harness the power of pattern matching.
💡 Key concept: Regular expressions are patterns that describe sets of strings. They're used for searching, extracting, and replacing text based on patterns rather than exact matches.
Regex Basics
Understanding the Language of Patterns
# Before we dive into the functions, let's understand the pattern language
import re
# 1. Literal characters - match exactly what you type
pattern = r"cat"
text = "The cat sat on the mat"
match = re.search(pattern, text)
print(match.group()) # cat
# 2. The dot (.) - matches any single character except newline
pattern = r"c.t"
print(re.search(r"c.t", "cat").group()) # cat
print(re.search(r"c.t", "cot").group()) # cot
print(re.search(r"c.t", "cut").group()) # cut
# 3. Character classes ([...]) - matches any character in the set
print(re.search(r"[aeiou]", "hello").group()) # e
print(re.findall(r"[aeiou]", "hello")) # ['e', 'o']
# 4. Ranges - specify a range of characters
print(re.findall(r"[a-z]", "Hello123")) # ['e', 'l', 'l', 'o']
print(re.findall(r"[0-9]", "Hello123")) # ['1', '2', '3']
# 5. Quantifiers - specify how many times something appears
# * - zero or more times
print(re.search(r"go*gle", "ggle").group()) # ggle
print(re.search(r"go*gle", "google").group()) # google
# + - one or more times
print(re.search(r"go+gle", "google").group()) # google
# ? - zero or one times
print(re.search(r"colou?r", "color").group()) # color
print(re.search(r"colou?r", "colour").group()) # colour
# {n} - exactly n times
print(re.search(r"a{3}", "aaabbb").group()) # aaa
# {n,} - at least n times
print(re.search(r"a{2,}", "aaabbb").group()) # aaa
# {n,m} - between n and m times
print(re.search(r"a{2,3}", "aaabbb").group()) # aaa
# 6. Anchors - match positions, not characters
# ^ - start of string
print(re.search(r"^Hello", "Hello World").group()) # Hello
# $ - end of string
print(re.search(r"World$", "Hello World").group()) # World
# 7. Special characters (need escaping with \)
# . ^ $ * + ? { } [ ] \ | ( )
print(re.search(r"\$", "Price: $10").group()) # $
Regex basics summary:
- . — any character (except newline)
- [abc] — any of a, b, or c
- [a-z] — any lowercase letter
- * — zero or more
- + — one or more
- ? — zero or one
- {n,m} — between n and m times
- ^ — start of string
- $ — end of string
Quick Check: What does the dot (.) match in regex? (Answer: Any single character except newline)
Searching with re.search()
Finding the First Match
# re.search() finds the first occurrence of a pattern
import re
# 1. Basic search
text = "The quick brown fox jumps over the lazy dog"
pattern = r"fox"
match = re.search(pattern, text)
if match:
print(f"Found: {match.group()}") # fox
print(f"Position: {match.start()}-{match.end()}") # 16-19
# 2. Extracting email addresses
text = "Contact us at support@example.com or sales@company.com"
match = re.search(r"\w+@\w+\.\w+", text)
if match:
print(f"Email: {match.group()}") # support@example.com
# 3. Finding phone numbers
text = "Call me at 555-123-4567 or 555-987-6543"
pattern = r"\d{3}-\d{3}-\d{4}"
match = re.search(pattern, text)
print(f"Phone: {match.group()}") # 555-123-4567
# 4. Using groups to extract specific parts
text = "Name: Alice, Age: 25"
pattern = r"Name: (\w+), Age: (\d+)"
match = re.search(pattern, text)
if match:
name = match.group(1)
age = match.group(2)
print(f"Name: {name}, Age: {age}")
# 5. Checking if a pattern exists
text = "Hello World"
if re.search(r"World", text):
print("Found 'World' in the text")
# 6. Case-insensitive search
text = "Hello World"
match = re.search(r"world", text, re.IGNORECASE)
if match:
print(f"Found: {match.group()}") # World
re.search() key points:
- Returns match object — if found, else None
- group() — get the matched text
- start()/end() — get match positions
- groups() — get captured groups
- Case-insensitive — use re.IGNORECASE
Quick Check: What does re.search() return if no match is found? (Answer: None)
Finding All Matches with re.findall()
Getting All Occurrences
# re.findall() returns all non-overlapping matches as a list
import re
# 1. Finding all numbers
text = "I have 3 apples, 5 oranges, and 10 bananas"
numbers = re.findall(r"\d+", text)
print(f"Numbers: {numbers}") # ['3', '5', '10']
# 2. Finding all words
text = "Hello world! How are you?"
words = re.findall(r"\w+", text)
print(f"Words: {words}") # ['Hello', 'world', 'How', 'are', 'you']
# 3. Finding all email addresses
text = "Email: alice@example.com, bob@company.com"
emails = re.findall(r"\w+@\w+\.\w+", text)
print(f"Emails: {emails}") # ['alice@example.com', 'bob@company.com']
# 4. Finding all phone numbers
text = "Call 555-123-4567 or 555-987-6543"
phones = re.findall(r"\d{3}-\d{3}-\d{4}", text)
print(f"Phones: {phones}") # ['555-123-4567', '555-987-6543']
# 5. Finding all dates
text = "Dates: 2024-01-15, 2024-02-20, 2024-03-25"
dates = re.findall(r"\d{4}-\d{2}-\d{2}", text)
print(f"Dates: {dates}") # ['2024-01-15', '2024-02-20', '2024-03-25']
# 6. Extracting specific parts with groups
text = "Name: Alice, Age: 25 | Name: Bob, Age: 30"
pairs = re.findall(r"Name: (\w+), Age: (\d+)", text)
print(f"Pairs: {pairs}") # [('Alice', '25'), ('Bob', '30')]
# 7. Finding all vowels
text = "Hello World"
vowels = re.findall(r"[aeiou]", text, re.IGNORECASE)
print(f"Vowels: {vowels}") # ['e', 'o', 'o']
re.findall() key points:
- Returns list — all matches as strings
- With groups — returns list of tuples
- Non-overlapping — matches don't overlap
- Great for extraction — pulling out specific data
Quick Check: What does re.findall() return? (Answer: A list of all non-overlapping matches)
Finding Matches with re.finditer()
Getting Match Objects for All Occurrences
# re.finditer() returns an iterator of match objects
import re
# 1. Finding all matches with position information
text = "The cat sat on the mat. The cat was happy."
for match in re.finditer(r"cat", text):
print(f"Found 'cat' at position {match.start()}-{match.end()}")
# Output:
# Found 'cat' at position 4-7
# Found 'cat' at position 29-32
# 2. Finding all numbers with their positions
text = "I have 3 apples, 5 oranges, and 10 bananas"
for match in re.finditer(r"\d+", text):
print(f"Number {match.group()} at position {match.start()}-{match.end()}")
# 3. Finding all words with their positions
text = "Hello world! How are you?"
for match in re.finditer(r"\w+", text):
print(f"Word: {match.group()} at {match.start()}-{match.end()}")
# 4. Using with groups
text = "Name: Alice, Age: 25 | Name: Bob, Age: 30"
pattern = r"Name: (\w+), Age: (\d+)"
for match in re.finditer(pattern, text):
name = match.group(1)
age = match.group(2)
print(f"Name: {name}, Age: {age}")
# 5. Counting matches
text = "Hello Hello Hello"
count = sum(1 for _ in re.finditer(r"Hello", text))
print(f"Count: {count}") # 3
re.finditer() key points:
- Returns iterator — memory efficient for large texts
- Match objects — gives full info for each match
- Position info — start() and end() methods
- Groups — can access captured groups
Quick Check: What is the advantage of finditer() over findall()? (Answer: It returns match objects with position information)
Replacing with re.sub()
Find and Replace with Patterns
# re.sub() replaces all occurrences of a pattern
import re
# 1. Basic replacement
text = "The cat sat on the mat"
result = re.sub(r"cat", "dog", text)
print(result) # The dog sat on the mat
# 2. Replacing all numbers
text = "I have 3 apples, 5 oranges, and 10 bananas"
result = re.sub(r"\d+", "X", text)
print(result) # I have X apples, X oranges, and X bananas
# 3. Using a replacement function
def double_number(match):
num = int(match.group())
return str(num * 2)
text = "I have 3 apples and 5 oranges"
result = re.sub(r"\d+", double_number, text)
print(result) # I have 6 apples and 10 oranges
# 4. Replacing multiple spaces with a single space
text = "Hello World How are you?"
result = re.sub(r"\s+", " ", text)
print(result) # Hello World How are you?
# 5. Replacing with groups
text = "John Doe, Jane Smith"
result = re.sub(r"(\w+) (\w+)", r"\2, \1", text)
print(result) # Doe, John Smith, Jane
# 6. Removing unwanted characters
text = "Hello! How are you? I'm fine."
result = re.sub(r"[!?.]", "", text)
print(result) # Hello How are you Im fine
# 7. Count replacements
text = "The cat and the dog"
result, count = re.subn(r"the", "a", text, flags=re.IGNORECASE)
print(f"Result: {result}, Count: {count}") # a cat and a dog, Count: 2
re.sub() key points:
- Replaces all — all occurrences by default
- Count parameter — limit number of replacements
- Function replacement — dynamic replacement logic
- Group references — use \1, \2 for captured groups
- re.subn() — returns tuple with count
Quick Check: What does re.sub() do? (Answer: Replaces all occurrences of a pattern with a replacement string)
Splitting with re.split()
Split Strings Using Patterns
# re.split() splits a string by a pattern import re # 1. Basic splitting by pattern text = "apple,banana,cherry" result = re.split(r",", text) print(result) # ['apple', 'banana', 'cherry'] # 2. Splitting by multiple delimiters text = "apple,banana;cherry:grape" result = re.split(r"[,;:]", text) print(result) # ['apple', 'banana', 'cherry', 'grape'] # 3. Splitting by whitespace text = "Hello World How are you?" result = re.split(r"\s+", text) print(result) # ['Hello', 'World', 'How', 'are', 'you?'] # 4. Splitting by numbers text = "I have 3 apples and 5 oranges" result = re.split(r"\d+", text) print(result) # ['I have ', ' apples and ', ' oranges'] # 5. Keeping the delimiters text = "apple,banana,cherry" result = re.split(r"(,)", text) print(result) # ['apple', ',', 'banana', ',', 'cherry'] # 6. Splitting with max splits text = "a,b,c,d,e" result = re.split(r",", text, maxsplit=2) print(result) # ['a', 'b', 'c,d,e']
re.split() key points:
- Pattern-based — split by any pattern
- Multiple delimiters — use character classes
- Keep delimiters — use capturing groups
- maxsplit — limit the number of splits
Quick Check: How do you split by multiple delimiters? (Answer: Use a character class like [,;:])
Using Groups
Extracting Specific Parts
# Groups let you extract specific parts of a match
import re
# 1. Basic groups
pattern = r"(\d{3})-(\d{3})-(\d{4})"
text = "Phone: 555-123-4567"
match = re.search(pattern, text)
if match:
area_code = match.group(1) # 555
prefix = match.group(2) # 123
number = match.group(3) # 4567
print(f"Area: {area_code}, Prefix: {prefix}, Number: {number}")
# 2. Named groups
pattern = r"(?P\d{3})-(?P\d{3})-(?P\d{4})"
text = "Phone: 555-123-4567"
match = re.search(pattern, text)
if match:
print(f"Area: {match.group('area')}") # 555
print(f"Prefix: {match.group('prefix')}") # 123
print(f"Number: {match.group('number')}") # 4567
# 3. Non-capturing groups (?:...)
pattern = r"(?:Name: )(\w+)"
text = "Name: Alice"
match = re.search(pattern, text)
if match:
print(f"Name: {match.group(1)}") # Alice
# 4. Groups with findall
text = "Name: Alice, Age: 25 | Name: Bob, Age: 30"
pattern = r"Name: (\w+), Age: (\d+)"
matches = re.findall(pattern, text)
for name, age in matches:
print(f"Name: {name}, Age: {age}")
# 5. Groups with substitution
text = "John Doe"
result = re.sub(r"(\w+) (\w+)", r"\2, \1", text)
print(result) # Doe, John
# 6. Nesting groups
pattern = r"((\d{3})-(\d{3})-(\d{4}))"
text = "Phone: 555-123-4567"
match = re.search(pattern, text)
if match:
print(f"Full: {match.group(1)}") # 555-123-4567
print(f"Area: {match.group(2)}") # 555
print(f"Prefix: {match.group(3)}") # 123
print(f"Number: {match.group(4)}") # 4567
Groups key points:
- () — capture group
- group(1) — first captured group
- named groups — (?P
...) - (?:...) — non-capturing group
- \1, \2 — backreferences in replacement
Quick Check: How do you create a named group? (Answer: (?P
Regex Flags
Modifying Regex Behavior
# Flags change how the regex engine works
import re
# 1. re.IGNORECASE (or re.I) - case-insensitive matching
text = "Hello World"
pattern = r"world"
match = re.search(pattern, text, re.IGNORECASE)
print(match.group()) # World
# 2. re.MULTILINE (or re.M) - ^ and $ match start/end of lines
text = """Line 1
Line 2
Line 3"""
matches = re.findall(r"^Line \d", text, re.MULTILINE)
print(matches) # ['Line 1', 'Line 2', 'Line 3']
# 3. re.DOTALL (or re.S) - . matches newline as well
text = "Hello\nWorld"
match = re.search(r"Hello.World", text, re.DOTALL)
print(match.group()) # Hello\nWorld
# 4. re.VERBOSE (or re.X) - allow whitespace and comments in pattern
pattern = r"""
\d{3} # Area code
- # Separator
\d{3} # Prefix
- # Separator
\d{4} # Number
"""
text = "555-123-4567"
match = re.search(pattern, text, re.VERBOSE)
print(match.group()) # 555-123-4567
# 5. Combining flags
text = "Hello\nWorld"
pattern = r"hello.world"
match = re.search(pattern, text, re.IGNORECASE | re.DOTALL)
print(match.group()) # Hello\nWorld
# 6. re.ASCII (or re.A) - make \w, \b, \s, \d match only ASCII
text = "café"
matches = re.findall(r"\w+", text) # ['café']
matches_ascii = re.findall(r"\w+", text, re.ASCII) # ['caf']
Common flags:
- re.IGNORECASE — case-insensitive
- re.MULTILINE — ^ and $ per line
- re.DOTALL — . matches newline
- re.VERBOSE — whitespace and comments
- Combine — use | (pipe) to combine flags
Quick Check: Which flag makes the pattern case-insensitive? (Answer: re.IGNORECASE)
Real-World Examples
Practical Regex Applications
# Here are some real-world regex examples
import re
# 1. Email validation
def is_valid_email(email):
pattern = r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"
return bool(re.match(pattern, email))
print(is_valid_email("user@example.com")) # True
print(is_valid_email("invalid-email")) # False
# 2. URL extraction
def extract_urls(text):
pattern = r"https?://[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}(?:/[^\s]*)?"
return re.findall(pattern, text)
text = "Visit https://example.com and http://test.com"
print(extract_urls(text)) # ['https://example.com', 'http://test.com']
# 3. Password validation
def is_valid_password(password):
# At least 8 chars, one uppercase, one lowercase, one digit
pattern = r"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$"
return bool(re.match(pattern, password))
print(is_valid_password("Password123")) # True
print(is_valid_password("pass")) # False
# 4. Extracting hashtags
def extract_hashtags(text):
return re.findall(r"#\w+", text)
text = "I love #Python and #coding"
print(extract_hashtags(text)) # ['#Python', '#coding']
# 5. Cleaning HTML tags
def remove_html_tags(text):
return re.sub(r"<[^>]+>", "", text)
html = "Hello World
"
print(remove_html_tags(html)) # Hello World
# 6. Extracting numbers with units
text = "The box is 10kg and the other is 5.5kg"
matches = re.findall(r"(\d+\.?\d*)kg", text)
print(matches) # ['10', '5.5']
# 7. Date validation (YYYY-MM-DD)
def is_valid_date(date):
pattern = r"^(20\d{2})-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$"
return bool(re.match(pattern, date))
print(is_valid_date("2024-01-15")) # True
print(is_valid_date("2024-13-01")) # False
# 8. Extracting Twitter handles
text = "Follow @alice and @bob on Twitter"
handles = re.findall(r"@\w+", text)
print(handles) # ['@alice', '@bob']
Common use cases:
- Email validation — check email format
- URL extraction — find links in text
- Password validation — enforce password rules
- Data extraction — pull specific information
- Text cleaning — remove unwanted tags
Quick Check: What pattern would you use to find hashtags? (Answer: #\w+)
Try It Yourself
Experiment with regular expressions in the editor below.
REGULAR EXPRESSIONS PRACTICE
========================================
1. SEARCHING
Found: fox at position 16
2. FINDING ALL
Numbers: ['3', '5', '10']
3. FINDING ALL WITH POSITIONS
Found 'at' at position 4
Found 'at' at position 18
4. REPLACING
Result: The dog sat on the mat
5. SPLITTING
Split result: ['apple', 'banana', 'cherry', 'grape']
6. EMAIL VALIDATION
'user@example.com': True
'invalid-email': False
Regular expressions practice complete!
You've Got It!
You now understand regular expressions in Python. You can search, find, replace, and split text using powerful patterns. Regex is a skill that will serve you well in data processing and text analysis.
Quick Quiz
Test what you've learned:
Frequently Asked Questions
What is the difference between re.search() and re.match()?
What is the difference between re.findall() and re.finditer()?
How do I make regex case-insensitive?
re.IGNORECASE flag (or re.I for short). For example: re.search(r"world", text, re.IGNORECASE). This makes the pattern match "World", "world", "WORLD", etc.
What's a common interview question about regex?
What is the difference between \d and \D?
Can I use regex to validate email addresses?
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ works for most cases. For full compliance with the email specification, you'd need a very complex regex or use a specialized library.
Where to Go From Here
Now that you understand regular expressions, check out these related topics:
Object-Oriented Programming
Learn about classes and objects in Python.
Learn More →Iterators
Learn about iterators and how they work.
Learn More →Generators
Learn about generators for memory-efficient data processing.
Learn More →