Back to articles
Regex Cheatsheet That Actually Makes Sense (With Real Examples)

Regex Cheatsheet That Actually Makes Sense (With Real Examples)

via Dev.to TutorialAlex Spinov

I've been writing regex for 8 years. I still google basic patterns. The problem isn't regex itself — it's that every cheatsheet shows you the theory without the practice. Here's the one I wish existed when I started. The Essentials (90% of what you'll ever need) Pattern Means Example Matches . Any character h.t hat, hit, hot \d Any digit \d{3} 123, 456 \w Letter, digit, _ \w+ hello, test_1 \s Whitespace \s+ spaces, tabs ^ Start of string ^Hello "Hello world" $ End of string end$ "the end" * 0 or more ab*c ac, abc, abbc + 1 or more ab+c abc, abbc ? 0 or 1 colou?r color, colour {n} Exactly n \d{4} 2026 {n,m} Between n and m \d{2,4} 12, 123, 1234 [abc] a, b, or c [aeiou] vowels [^abc] Not a, b, or c [^0-9] non-digits (...) Capture group (\d+)px captures "12" from "12px" `\ ` Or `cat\ Real-World Patterns (Copy-Paste Ready) Email Validation {% raw %} import re # Simple but effective pattern = r ' ^[\w.+-]+@[\w-]+\.[\w.]+$ ' re . match ( pattern , ' user@example.com ' ) # ✓ re . match ( patt

Continue reading on Dev.to Tutorial

Opens in a new tab

Read Full Article
2 views

Related Articles