I've been collecting regex patterns for years. Most regex tutorials teach you theory — here are the ones I actually paste into production code.
Email Validation (The Real One)
import re
email_re = re.compile(r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$')
Not RFC 5322 compliant (nothing practical is), but catches 99% of valid emails and rejects obvious junk.
URL Extraction
url_re = re.compile(r'https?://[^\s<>"{}|\\^`\[\]]+')
urls = url_re.findall(text)
Phone Numbers (US)
phone_re = re.compile(r'\b(?:\+?1[-.]?)?\(?\d{3}\)?[-.]?\d{3}[-.]?\d{4}\b')
Handles: (555) 123-4567, 555-123-4567, +1.555.123.4567
IP Addresses (v4)
ip_re = re.compile(r'\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b')
Date Extraction (Multiple Formats)
date_re = re.compile(r'\b\d{1,2}[/.-]\d{1,2}[/.-]\d{2,4}\b|\b\d{4}[/.-]\d{2}[/.-]\d{2}\b')
Matches: 12/25/2024, 2024-01-15, 3.14.24
HTML Tag Stripping
strip_tags = re.compile(r'<[^>]+>')
clean = strip_tags.sub('', html_string)
Password Strength Check
strong_pw = re.compile(r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$')
At least 8 chars, one upper, one lower, one digit, one special.
CSV Line Parser (Handles Quoted Commas)
csv_re = re.compile(r',(?=(?:[^"]*"[^"]*")*[^"]*$)')
fields = csv_re.split(line)
Log Level Extraction
log_re = re.compile(r'\b(DEBUG|INFO|WARNING|ERROR|CRITICAL)\b')
Whitespace Normalizer
normalize = re.compile(r'\s+')
clean = normalize.sub(' ', messy_text).strip()
These are 10 of the 50 patterns in my Regex Pattern Cookbook. The full cookbook covers: data validation, text extraction, log parsing, web scraping selectors, file path manipulation, and code analysis patterns.
Each pattern includes the regex, a plain-English explanation of what it does, and example input/output. $9 for the complete set.
Also check out: Python Automation Toolkit — 10 standalone scripts for common dev tasks.
Top comments (0)