Regex Rescue Card: The Only Regex Cheat Sheet You'll Actually Use (And Remember)

Regex Rescue Card: The Only Regex Cheat Sheet You'll Actually Use (And Remember)

Stop googling regex patterns every time you need to validate an email or extract data. This cheat sheet gives you the 10 patterns you'll actually use, with language-specific implementations and performance tips.

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

\d = digit (0-9)
\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

^ = start of string
$ = 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)

// Good enough for most forms
/^[^\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)

// US/Canada with optional formatting
/^\+?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')

// Basic URL validation
/^https?:\/\/[\w\-]+(\.[\w\-]+)+[\/#?]?.*$/i

// Extract domain only
/https?:\/\/([\w\-]+(?:\.[\w\-]+)+)/i

đź“… Dates (Because Everyone Formats Them Differently)

// YYYY-MM-DD (ISO)
/^\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

// Find anything that looks like money
/\$\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).

Discussion

Add a comment

0/5000
Loading comments...