re module.
Getting Started
Compiling Patterns
Compile a regular expression pattern into a pattern object for reuse:Basic Matching
Match patterns at the beginning of strings:Pattern Syntax
Character Classes
Match specific sets of characters:[abc]- matches ‘a’, ‘b’, or ‘c’[a-z]- matches any lowercase letter[^5]- matches any character except ‘5’
Special Sequences
Pre-defined character sets:\d- any decimal digit[0-9]\D- any non-digit[^0-9]\s- any whitespace character\S- any non-whitespace character\w- any alphanumeric character[a-zA-Z0-9_]\W- any non-alphanumeric character
Repetition
Specify how many times to match:*- 0 or more times+- 1 or more times?- 0 or 1 time{m,n}- at least m, at most n times
Searching and Finding
import re
p = re.compile(r'\d+')
text = 'There are 12 drummers and 11 pipers'
# match() - checks beginning only
p.match(text) # Returns None
# search() - finds first match anywhere
m = p.search(text)
m.group() # Returns '12'
# findall() - returns all matches as list
p.findall(text) # Returns ['12', '11']
# finditer() - returns iterator of match objects
for match in p.finditer(text):
print(match.group(), 'at position', match.start())
Grouping
Capture parts of the pattern:Named Groups
Use names instead of numbers:Backreferences
Match repeated patterns:String Modification
Splitting
Split strings on pattern matches:Substitution
Replace pattern matches:Compilation Flags
Modify pattern behavior:Common Patterns
Email Validation
Phone Numbers
URL Extraction
Best Practices
When NOT to Use Regex: For simple string operations, use string methods:
Common Gotchas
Greedy vs Non-Greedy
By default, repetition is greedy:Anchors Matter
Reference
Keyre module functions:
compile(pattern, flags=0)- Compile a patternmatch(pattern, string, flags=0)- Match at string startsearch(pattern, string, flags=0)- Search anywherefindall(pattern, string, flags=0)- Find all matchessub(pattern, repl, string, count=0, flags=0)- Replace matchessplit(pattern, string, maxsplit=0, flags=0)- Split on pattern
