The Basics
.— any character*— zero or more of the previous+— one or more of the previous?— zero or one of the previous\d— any digit\w— any word character (letter, digit, underscore)\s— any whitespace
Useful Patterns
Email: [\w.-]+@[\w.-]+\.\w+
URL: https?://[\w.-]+(/[\w.-]*)*
Phone: \+?\d{1,3}[-.\s]?\d{3,4}[-.\s]?\d{4}
IPv4: \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}
Date: \d{4}-\d{2}-\d{2}Testing Regex
Test your patterns with our Regex Tester before using them in code. It highlights matches in real-time and explains what each part of the pattern does.
Common Mistakes
- Not escaping special characters (
.matches ANY character, use\.for literal dot) - Greedy matching capturing too much (use
.*?for non-greedy) - Forgetting anchors (
^start,$end) — without them, partial matches count