Regular expressions (regex) are patterns used to match character combinations in strings. They provide a concise way to search, extract, and manipulate text based on specific rules.
Using the re Module
Python's re module provides regex functionality:
import re
# Match a pattern at string start
match_result = re.match(r'pattern', 'input_string')
# Extract matched substring
if match_result:
print(match_result.group())
Basic Match Example
import re
result = re.match(r'itcast', 'itcast.cn')
print(result.group()) # Output: itcast
Character Matching Patterns
| Pattern | Matches |
|---|---|
. |
Any character except newline |
[abc] |
Any character in brackets |
\d |
Digit (0-9) |
\D |
Non-digit |
\s |
Whitespace |
\S |
Non-whitespace |
\w |
Word character (a-z, A-Z, 0-9, _) |
\W |
Non-word character |
Pattern Examples
import re
# Dot matches any character
print(re.match(r'.', 'a').group()) # a
# Character set matching
print(re.match(r'[hH]', 'Hello').group()) # H
print(re.match(r'[0-9]', '7Up').group()) # 7
# Digit matching
print(re.match(r'嫦娥\d号', '嫦娥1号发射成功').group()) # 嫦娥1号
Raw Strings
Use raw strings (prefix r) to avoid excessive escaping:
path = r"c:\a\b\c"
print(path) # c:\a\b\c
# Proper regex escaping
match = re.match(r"c:\\a", path)
print(match.group()) # c:\a
Quantifiers
| Pattern | Matches |
|---|---|
* |
0 or more repetitions |
+ |
1 or more repetitions |
? |
0 or 1 repetition |
{n} |
Exactly n repetitions |
{n,} |
n or more repetitions |
{n,m} |
Between n and m repetitions |
Quantifier Examples
# Zero or more lowercase letters
print(re.match(r'[A-Z][a-z]*', 'M').group()) # M
# Valid variable names
print(re.match(r'[a-zA-Z_]+\w*', '_name').group()) # _name
# Numbers 0-99
print(re.match(r'[1-9]?\d', '33').group()) # 33
# Password length (8-20 chars)
print(re.match(r'[\w]{8,20}', 'abc123_xyz').group())
Boundary Matching
| Pattern | Matches |
|---|---|
^ |
Start of string |
$ |
End of string |
\b |
Word boundary |
\B |
Non-word boundary |
Boundary Examples
# Exact domain match
match = re.match(r'[\w]{4,20}@163\.com$', 'user@163.com')
print(match.group())
# Word boundary
print(re.search(r'\bver\b', 'ho ver').group()) # ver
Grouping
| Pattern | Functionality |
|---|---|
| `a | b` |
(ab) |
Capture group |
\n |
Backreference to group n |
(?P<name>...) |
Named group |
(?P=name) |
Reference named group |
Grouping Examples
# Match 0-100
print(re.match(r'100|[1-9]?\d$', '100').group()) # 100
# Email provider extraction
match = re.match(r'\w{4,20}@(163|126|qq)\.com', 'test@126.com')
print(match.group(1)) # 126
# Backreference for HTML tags
match = re.match(r'<(\w+)>.*</\1>', '<title>Text</title>')
print(match.group()) # <title>Text</title>
# Named groups
html_match = re.match(
r"<(?P<tag1>\w+)><(?P<tag2>\w+)>.*</(?P=tag2)></(?P=tag1)>",
"<div><span>Content</span></div>"
)
print(html_match.group())
Advanced re Methods
search()
Find first occurrence anywhere in string:
match = re.search(r'\d+', 'Views: 9999')
print(match.group()) # 9999
findall()
Find all non-overlapping matches:
numbers = re.findall(r'\d+', 'Python=9999, C=7890, C++=12345')
print(numbers) # ['9999', '7890', '12345']
sub()
Replace matched patterns:
# Simple replacement
print(re.sub(r'\d+', '998', 'Python=997')) # Python=998
# Function-based replacement
def increment(match):
return str(int(match.group()) + 1)
print(re.sub(r'\d+', increment, 'Python=99')) # Python=100
split()
Split string using pattern:
tokens = re.split(r":|", "info:xiaoZhang 33 shandong")
print(tokens) # ['info', 'xiaoZhang', '33', 'shandong']
Greedy vs Non-Greedy Matching
Quantifiers are greedy by default. Add ? to make them non-greedy:
# Greedy match
greedy = re.match(r'.+(\d+-\d+-\d+-\d+)', "ID: 234-235-22-423")
print(greedy.group(1)) # '4-235-22-423'
# Non-greedy match
non_greedy = re.match(r'.+?(\d+-\d+-\d+-\d+)', "ID: 234-235-22-423")
print(non_greedy.group(1)) # '234-235-22-423'