Regular Expressions: A Practical Guide with Examples
· 10 min read
Regex Basics
| Pattern | Matches | Example |
|---|---|---|
. | Any character (except newline) | a.c matches abc, a1c, a-c |
\d | Digit [0-9] | \d{3} matches 123, 456 |
\w | Word char [a-zA-Z0-9_] | \w+ matches hello, user_1 |
\s | Whitespace | \s+ matches spaces, tabs |
^ | Start of string/line | ^Hello matches Hello at start |
$ | End of string/line | world$ matches world at end |
[abc] | Character class | [aeiou] matches any vowel |
[^abc] | Negated class | [^0-9] matches non-digits |
\b | Word boundary | \bcat\b matches cat not category |
Quantifiers
| Quantifier | Meaning | Example |
|---|---|---|
* | 0 or more | ab*c matches ac, abc, abbc |
+ | 1 or more | ab+c matches abc, abbc (not ac) |
? | 0 or 1 | colou?r matches color, colour |
{n} | Exactly n | \d{4} matches 2026 |
{n,m} | Between n and m | \d{2,4} matches 12, 123, 1234 |
*? | Lazy (non-greedy) | <.*?> matches single HTML tags |
Groups and Captures
// Capturing group
/(\d{4})-(\d{2})-(\d{2})/
// Matches: 2026-03-29
// Group 1: 2026, Group 2: 03, Group 3: 29
// Named group
/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/
// Non-capturing group
/(?:https?|ftp):\/\//
// Alternation
/cat|dog|bird/
// Lookahead (positive)
/\d+(?= dollars)/ // matches 100 in "100 dollars"
// Lookbehind (positive)
/(?<=\$)\d+/ // matches 100 in "$100"
Common Patterns
| Pattern | Regex |
|---|---|
| Email (simple) | ^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,}$ |
| URL | ^https?:\/\/[\w.-]+(?:\.[a-zA-Z]{2,})(?:\/\S*)?$ |
| IPv4 | ^(?:\d{1,3}\.){3}\d{1,3}$ |
| Date (YYYY-MM-DD) | ^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$ |
| Phone (US) | ^\+?1?[-.\s]?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$ |
| Hex color | ^#(?:[0-9a-fA-F]{3}){1,2}$ |
| Strong password | ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$ |
| Slug | ^[a-z0-9]+(?:-[a-z0-9]+)*$ |
Test your patterns with our Regex Tester.
Flags
| Flag | Name | Effect |
|---|---|---|
g | Global | Find all matches, not just the first |
i | Case-insensitive | /hello/i matches Hello, HELLO |
m | Multiline | ^ and $ match line boundaries |
s | Dotall | . matches newlines too |
u | Unicode | Correct handling of Unicode characters |
Performance Tips
- Avoid catastrophic backtracking:
(a+)+is dangerous, usea+instead - Be specific:
[a-z]+is faster than.+ - Use non-capturing groups
(?:...)when you don't need the capture - Anchor patterns with
^and$when validating full strings - Use possessive quantifiers
a++or atomic groups(?>...)when available
Frequently Asked Questions
What is the difference between * and + in regex?
* matches zero or more occurrences (the preceding element is optional). + matches one or more (at least one occurrence is required).
How do I match a literal dot?
Escape it with a backslash: \. matches a literal period. Without the backslash, . matches any character.
What is a non-greedy (lazy) match?
Add ? after a quantifier to make it lazy: .*? matches as few characters as possible, while .* matches as many as possible.
Should I use regex to parse HTML?
No. HTML is not a regular language. Use a proper HTML parser (DOMParser, cheerio, BeautifulSoup). Regex is fine for simple pattern extraction but fails on nested structures.