Search, match, and replace text patterns with Python’s re module — the practical patterns you’ll actually use.
Why: a regular expression (regex) describes a pattern of text. re.search() scans a string and returns a match object if the pattern appears anywhere — or None if it does not. Write patterns as a raw string (an r before the quote) so backslashes are taken literally.
import re
text = 'Order 12345 shipped'
match = re.search(r'\d+', text) # \d+ = one or more digits
if match:
print(match.group()) # '12345' (the matched text)Why: a handful of symbols cover most real needs. \d is a digit, \w is a letter/number/underscore, \s is whitespace, . is any character, + means "one or more", * means "zero or more", and {2,4} means "between 2 and 4".
import re
# a simple email-ish pattern
pattern = r'\w+@\w+\.\w+'
print(bool(re.search(pattern, 'reach me at a@x.com'))) # True
# anchors: ^ start, $ end
print(bool(re.match(r'^\d{4}$', '2024'))) # True (exactly 4 digits)Why: re.findall() returns every match as a list, and re.sub() replaces all matches with new text. These two cover most day-to-day text cleaning.
import re
text = 'Call 555-1234 or 555-5678'
print(re.findall(r'\d{3}-\d{4}', text))
# ['555-1234', '555-5678']
# mask the numbers
print(re.sub(r'\d{3}-\d{4}', '[hidden]', text))
# 'Call [hidden] or [hidden]'Why: parentheses ( ) capture part of a match so you can pull it out separately. group(1) is the first captured piece — handy for splitting structured text like dates.
import re
match = re.search(r'(\d{4})-(\d{2})-(\d{2})', '2024-03-15')
if match:
print(match.group(1)) # '2024' (year)
print(match.group(2)) # '03' (month)
print(match.groups()) # ('2024', '03', '15')