A Developer's Guide to Regular Expressions
Regular expressions (regex) are powerful pattern matching tools. This guide covers what you need to know.
The Basics
| Pattern | Description | Example Match |
|---------|-------------|---------------|
| . | Any character | a, b, 1, ! |
| * | Zero or more | a* matches "", "a", "aaa" |
| + | One or more | a+ matches "a", "aaa" |
| ? | Optional | colou?r matches "color", "colour" |
| \d | Digit | \d+ matches "123" |
| \w | Word char | \w+ matches "hello" |
| \s | Whitespace | matches spaces, tabs |
Common Patterns
Email Validation
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
URL Matching
https?://[^\s]+
Phone Numbers
\+?[\d\s-]{10,}
IP Addresses
\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
Practical Tips
- Start simple - Build patterns incrementally
- Use online testers - Try our Regex Tester
- Escape special chars - Use
\before.,*,+,?,[,],(,),{,},^,$,|,\ - Be specific -
\d+is better than.+for numbers - Test edge cases - Empty strings, special characters, long inputs
Greedy vs Lazy
.*is greedy - matches as much as possible.*?is lazy - matches as little as possible
Example: For <b>bold</b>, <.*> matches <b>bold</b>, but <.*?> matches <b>.
Conclusion
Regex has a steep learning curve but is invaluable for text processing. Bookmark our Regex Tester and Regex Cheatsheet for quick reference.
Try our free developer tools
All tools run in your browser with zero data uploads.