re module
re.compile(pattern, flags=0) The re module provides regular expression operations. It supports pattern matching, searching, replacing, and splitting text using regular expression patterns.
Syntax
import re
Core Functions
re.compile()
Compiles a regular expression pattern into a regex object for reuse.
import re
pattern = re.compile(r"\d{3}-\d{4}")
result = pattern.search("Call me at 555-1234 tomorrow")
print(result.group())
# 555-1234
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
pattern | str | Required | The regular expression pattern to compile |
flags | int | 0 | Bitwise flags that modify pattern behavior |
re.search()
Searches for the first match of a pattern in a string.
text = "The price is $19.99"
match = re.search(r"\$([0-9]+\.[0-9]+)", text)
if match:
print(match.group(1))
# 19.99
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
pattern | str | Required | The regular expression pattern |
string | str | Required | The string to search |
flags | int | 0 | Bitwise flags that modify pattern behavior |
re.match()
Matches only at the beginning of the string.
text = "hello world"
match = re.match(r"hello", text)
print(match.group() if match else "No match")
# hello
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
pattern | str | Required | The regular expression pattern |
string | str | Required | The string to search |
flags | int | 0 | Bitwise flags that modify pattern behavior |
re.findall()
Returns all non-overlapping matches as a list.
text = "There are 3 cats and 5 dogs"
numbers = re.findall(r"\d+", text)
print(numbers)
# [3, 5]
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
pattern | str | Required | The regular expression pattern |
string | str | Required | The string to search |
flags | int | 0 | Bitwise flags that modify pattern behavior |
re.finditer()
Returns an iterator of all match objects.
text = "Split this: one, two, three"
for match in re.finditer(r"\w+", text):
print(f"Found {match.group()} at {match.start()}")
# Found Split at 0
# Found this at 6
# Found one at 12
# Found two at 16
# Found three at 21
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
pattern | str | Required | The regular expression pattern |
string | str | Required | The string to search |
flags | int | 0 | Bitwise flags that modify pattern behavior |
re.sub()
Replaces all matches with a replacement string.
text = "The cat sat on the mat"
result = re.sub(r"cat", "dog", text)
print(result)
# The dog sat on the mat
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
pattern | str | Required | The regular expression pattern |
repl | str | Required | The replacement string (can include backreferences) |
string | str | Required | The string to search |
count | int | 0 | Maximum number of replacements (0 = all) |
flags | int | 0 | Bitwise flags that modify pattern behavior |
re.subn()
Like sub() but returns a tuple with the replacement count.
text = "aaa bbb aaa ccc"
result, count = re.subn(r"aaa", "xxx", text)
print(f"Result: {result}, Count: {count}")
# Result: xxx bbb xxx ccc, Count: 2
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
pattern | str | Required | The regular expression pattern |
repl | str | Required | The replacement string |
string | str | Required | The string to search |
count | int | 0 | Maximum number of replacements |
flags | int | 0 | Bitwise flags that modify pattern behavior |
re.split()
Splits the string by matches of the pattern.
text = "apple, banana; orange: grape"
parts = re.split(r"[,;: ]+", text)
print(parts)
# [apple, banana, orange, grape]
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
pattern | str | Required | The regular expression pattern |
string | str | Required | The string to split |
maxsplit | int | 0 | Maximum number of splits |
flags | int | 0 | Bitwise flags that modify pattern behavior |
Match Object Methods
The methods below are called on Match objects returned by functions like re.search(), re.match(), and re.finditer().
match.group()
Returns the matched string or a specific captured group.
pattern = re.compile(r"(\w+)@(\w+)\.(\w+)")
match = pattern.search("user@example.com")
print(match.group()) # user@example.com
print(match.group(1)) # user
print(match.group(2)) # example
print(match.group(3)) # com
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
group | int or str | 0 | The group number (0 = entire match) or named group string |
match.groups()
Returns all captured groups as a tuple.
pattern = re.compile(r"(\w+)-(\d+)")
match = pattern.search("abc-123")
print(match.groups())
# (abc, 123)
Common Patterns
Email validation
email = "user@example.com"
pattern = r"^[\w.-]+@[\w.-]+\.\w+$"
if re.match(pattern, email):
print("Valid email")
# Valid email
Phone number extraction
text = "Call 555-123-4567 or 555.987.6543"
phones = re.findall(r"\d{3}[-. ]\d{3}[-. ]\d{4}", text)
print(phones)
# [555-123-4567, 555.987.6543]
Named groups
pattern = r"(?P<name>\w+) (?P<age>\d+)"
text = "John 30"
match = re.search(pattern, text)
print(match.group("name"))
# John
print(match.group("age"))
# 30
Pattern Flags
Use flags to modify regex behavior:
text = "Hello\nWorld"
# Case-insensitive matching
print(re.search(r"hello", text, re.I).group())
# Hello
# Multiline mode
print(re.search(r"^World", text, re.M).group())
# World
# Dot matches newlines
print(re.search(r".+", text, re.DOTALL).group())
# Hello\nWorld
Errors
| Error | When it occurs |
|---|---|
re.error | Invalid regex pattern syntax |
None | re.search()/re.match() returns None when no match found |