โก Quick Reference
The 5 regex patterns you'll use 90% of the time, ready to copy-paste.
/^[^\s@]+@[^\s@]+\.[^\s@]+$/i
// Phone (US/Canada)
/^\+?1?[-.\s]?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$/
// URL (http/https)
/^https?:\/\/[\w\-]+(\.[\w\-]+)+[\/#?]?.*$/i
// Date (YYYY-MM-DD)
/^\d{4}-\d{2}-\d{2}$/
// Extract hashtags
/#([a-zA-Z0-9_]+)/g
Because 'Just Google It' Is a Career-Long Lie
You've been there. It's 3 AM, you're validating user input for the 47th time this month, and you're staring at Stack Overflow wondering why regex syntax evaporates from your brain the moment you close the tab. The "just Google it" advice stopped being funny around the same time you realized you'd spent more hours searching for regex patterns than actually writing code.
This isn't about memorizationโit's about efficiency. You don't need to remember everything. You just need to remember the right things, organized in a way that makes sense when you're bleary-eyed and deadline-drunk.
๐ TL;DR: What This Card Actually Does
- Gives you the 10 patterns you'll actually use (not academic exercises)
- Shows language-specific quirks so your regex works in JS/Python/Go/Bash
- Includes performance tips so you don't accidentally DDOS yourself
The Core Syntax You Actually Need
Forget the textbook. Here's what matters when you're trying to extract data or validate input before your coffee kicks in.
๐ Character Classes & Quantifiers
\w = word char (a-z, A-Z, 0-9, _)
\s = whitespace
. = any char except newline
? = 0 or 1 time
* = 0 or more times
+ = 1 or more times
{n} = exactly n times
{n,} = n or more times
{n,m} = between n and m times
๐ Anchors & Groups
$ = end of string
() = capturing group
(?:) = non-capturing group
| = OR operator
[] = character set
[^] = negated character set
Real-World Patterns (Copy-Paste Ready)
These aren't perfect academic solutionsโthey're practical patterns that work for 95% of real applications.
๐ง Email Validation (The One You Always Forget)
/^[^\s@]+@[^\s@]+\.[^\s@]+$/i
// More strict (but still not RFC perfect)
/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
Stop trying to validate every possible email format. The first pattern catches 99% of real emails while rejecting obvious garbage.
๐ Phone Numbers (Because Formats Vary Wildly)
/^\+?1?[-.\s]?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$/
// International (simplified)
/^\+[1-9]\d{1,14}$/
Pro tip: Consider stripping all non-digits first, then validating length. Users will enter phone numbers in ways that defy logic.
๐ URLs (When You Need More Than Just 'http')
/^https?:\/\/[\w\-]+(\.[\w\-]+)+[\/#?]?.*$/i
// Extract domain only
/https?:\/\/([\w\-]+(?:\.[\w\-]+)+)/i
๐ Dates (Because Everyone Formats Them Differently)
/^\d{4}-\d{2}-\d{2}$/
// MM/DD/YYYY or DD/MM/YYYY
/^\d{1,2}[\/\-]\d{1,2}[\/\-]\d{4}$/
// Extract dates from text
/\b\d{1,2}[\/\-]\d{1,2}[\/\-]\d{2,4}\b/g
Remember: Regex validates format, not logic. 2023-02-30 will pass. Always follow up with actual date parsing.
Language-Specific Gotchas
Because nothing hurts more than a regex that works in Python but explodes in JavaScript.
| Language | Key Difference | Example |
|---|---|---|
| JavaScript | No lookbehind in older browsers | /(?<!not)allowed/ // ES2018+ only |
| Python | Raw strings recommended | r'\d+' not '\\d+' |
| Go | Must escape backslashes twice | "\\d+" not "\d+" |
| Bash (grep) | Basic vs extended regex | grep -E for +, ?, | |
๐จ Emergency Patterns (I Need This NOW)
When you're in panic mode and just need something that works.
Copy-Paste Emergency Kit
/\$\d+(?:\.\d{2})?/g
// Match whole words (case-insensitive)
/\bword\b/gi
// Split on multiple delimiters
/[,\s;]+/
// Remove HTML tags (basic)
/<[^>]*>/g
// Find duplicate words
/\b(\w+)\s+\1\b/gi
๐ก Pro Tips That Actually Matter
Performance & Practicality
- Anchors are your friends: Use ^ and $ when possibleโthey prevent catastrophic backtracking
- Be specific: \d is faster than [0-9], \w is faster than [a-zA-Z0-9_]
- Lazy vs greedy: .*? stops at first match, .* eats everything (often too much)
- Test online: regex101.com shows step-by-step execution and explains what each part does
- When to bail: If your regex is 3 lines long, consider writing a parser instead
Stop Searching, Start Building
Print this. Bookmark it. Tape it to your monitor. The goal isn't to become a regex wizardโit's to stop wasting mental energy on syntax you'll forget again tomorrow. These patterns cover 90% of what you actually need in day-to-day development.
Next time you need to validate an email, extract a phone number, or parse some messy data, you won't be heading to Google. You'll be reaching for this card, getting it done in 30 seconds, and moving on to the actual hard problems that deserve your brainpower.
Action item: Save the Quick Reference box at the top. That's your emergency kit. The rest is context for when you need to tweak or understand why something works (or doesn't).
Quick Summary
- What: Developers constantly forget regex syntax and waste time searching for the same patterns over and over, especially for validation, parsing, and data cleaning tasks
๐ฌ Discussion
Add a Comment