Regular expressions have a reputation for being cryptic and intimidating. And yeah, something like ^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d@$!%*?&]{8,}$ looks like someone fell asleep on the keyboard. But once you learn the building blocks, even complex patterns make sense.
The Building Blocks
# Literal characters match themselves
/hello/ matches "hello" in "say hello world"
# Special characters
. Any single character
\d Any digit (0-9)
\w Any word character (letter, digit, underscore)
\s Any whitespace (space, tab, newline)
# Quantifiers
* Zero or more
+ One or more
? Zero or one (optional)
{3} Exactly 3 times
{2,5} Between 2 and 5 times
# Anchors
^ Start of string
$ End of string
Patterns You’ll Actually Use
Email Validation (Simple)
import re
pattern = r'^[\w.-]+@[\w.-]+\.\w{2,}$'
re.match(pattern, '[email protected]') # Match
re.match(pattern, 'not-an-email') # No match
This isn’t RFC-perfect, but it catches 99% of obviously invalid emails. For production, use a dedicated email validation library.
Extract Numbers from Text
text = "Order #12345 shipped 3 items for $49.99"
numbers = re.findall(r'\d+\.?\d*', text)
# Result: ['12345', '3', '49.99']
Phone Number Matching
pattern = r'\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}'
# Matches: (555) 123-4567, 555-123-4567, 5551234567, 555.123.4567
URL Extraction
text = "Visit https://example.com or http://test.org/page for info"
urls = re.findall(r'https?://[\w.-]+(?:/[\w.-]*)*', text)
# Result: ['https://example.com', 'http://test.org/page']
Groups and Capture
# Parentheses create groups you can extract
date_text = "Date: 2025-03-15"
match = re.search(r'(\d{4})-(\d{2})-(\d{2})', date_text)
year = match.group(1) # '2025'
month = match.group(2) # '03'
day = match.group(3) # '15'
# Named groups (more readable)
match = re.search(r'(?P\d{4})-(?P\d{2})-(?P\d{2})', date_text)
match.group('year') # '2025'
Find and Replace
# Replace all dates from MM/DD/YYYY to YYYY-MM-DD
text = "Born 12/25/1990, graduated 05/15/2012"
result = re.sub(r'(\d{2})/(\d{2})/(\d{4})', r'\3-\1-\2', text)
# Result: "Born 1990-12-25, graduated 2012-05-15"
Tips for Writing Regex
- Use regex101.com. It explains each part of your pattern and lets you test live
- Start simple and build up. Get a basic match working, then handle edge cases
- Comment complex patterns. Python’s re.VERBOSE flag lets you add whitespace and comments
- Don’t use regex for everything. HTML parsing? Use BeautifulSoup. JSON? Use json.loads(). Regex is for patterns in flat text.
You don’t need to memorize everything. Keep this page bookmarked and refer to it when you need a pattern. Regex is a tool – you just need to know it exists and how to wield it when the situation calls for it.
