Regular Expressions Made Easy: A Developer's Quick Reference
Regular expressions (regex) are one of the most powerful tools in a developer's toolkit — and one of the most intimidating. But they don't have to be. This guide breaks regex down into practical, understandable pieces with patterns you can use immediately.
Test any of these patterns yourself with our Regex Tester.
What Are Regular Expressions?
A regular expression is a pattern that describes a set of strings. You define a pattern, and the regex engine finds text that matches it. They're used for:
- Validation — checking if input matches a format (emails, phone numbers)
- Search — finding patterns in text
- Extraction — pulling specific data from strings
- Replacement — transforming text based on patterns
Basic Syntax
Literal Characters
Most characters match themselves:
Pattern: hello
Matches: "hello world" → "hello"Special Characters (Metacharacters)
These characters have special meaning and need escaping with \ to match literally:
. ^ $ * + ? { } [ ] \ | ( )To match a literal period: \.
To match a literal dollar sign: \$
Character Classes
Character classes match one character from a set:
| Pattern | Meaning | Example Match |
|---|---|---|
[abc] |
a, b, or c | "a" |
[a-z] |
Any lowercase letter | "m" |
[A-Z] |
Any uppercase letter | "M" |
[0-9] |
Any digit | "7" |
[a-zA-Z0-9] |
Any alphanumeric | "k" |
[^abc] |
NOT a, b, or c | "d" |
Shorthand Character Classes
| Pattern | Equivalent | Meaning |
|---|---|---|
\d |
[0-9] |
Any digit |
\D |
[^0-9] |
Any non-digit |
\w |
[a-zA-Z0-9_] |
Any word character |
\W |
[^a-zA-Z0-9_] |
Any non-word character |
\s |
[ \t\n\r\f] |
Any whitespace |
\S |
[^ \t\n\r\f] |
Any non-whitespace |
. |
(almost all) | Any character except newline |
Quantifiers
Quantifiers specify how many times a pattern should repeat:
| Pattern | Meaning | Example |
|---|---|---|
* |
0 or more times | a* matches "", "a", "aaa" |
+ |
1 or more times | a+ matches "a", "aaa" but not "" |
? |
0 or 1 time | a? matches "" or "a" |
{3} |
Exactly 3 times | a{3} matches "aaa" |
{2,5} |
2 to 5 times | a{2,5} matches "aa" to "aaaaa" |
{2,} |
2 or more times | a{2,} matches "aa", "aaa", etc. |
Greedy vs Lazy
By default, quantifiers are greedy — they match as much as possible. Add ? to make them lazy (match as little as possible):
Text: <div>hello</div><div>world</div>
Greedy: <div>.*</div> → matches entire string
Lazy: <div>.*?</div> → matches "<div>hello</div>"Anchors
Anchors match positions, not characters:
| Pattern | Meaning |
|---|---|
^ |
Start of string (or line with m flag) |
$ |
End of string (or line with m flag) |
\b |
Word boundary |
\B |
Not a word boundary |
Pattern: ^\d{3}$
Matches: "123" (exactly 3 digits, nothing else)
Does not match: "1234" or "abc123"Groups and Capturing
Capturing Groups ()
Parentheses create groups that capture matched text:
Pattern: (\d{4})-(\d{2})-(\d{2})
Input: "2026-02-05"
Group 1: "2026"
Group 2: "02"
Group 3: "05"Non-Capturing Groups (?:)
Groups that don't capture (useful for applying quantifiers):
Pattern: (?:https?://)?www\.example\.com
Matches: "www.example.com" or "https://www.example.com"Named Groups (?<name>)
Give groups meaningful names:
Pattern: (?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})Alternation (OR)
The pipe | acts as OR:
Pattern: cat|dog|fish
Matches: "cat", "dog", or "fish"Combine with groups for more specific OR:
Pattern: (Mr|Mrs|Ms)\.?\s\w+
Matches: "Mr Smith", "Mrs. Jones", "Ms Taylor"Lookahead and Lookbehind
These match a position based on what comes before or after, without including that text in the match:
| Pattern | Name | Meaning |
|---|---|---|
(?=...) |
Positive lookahead | Followed by ... |
(?!...) |
Negative lookahead | NOT followed by ... |
(?<=...) |
Positive lookbehind | Preceded by ... |
(?<!...) |
Negative lookbehind | NOT preceded by ... |
Pattern: \d+(?= dollars)
Input: "Price is 50 dollars"
Match: "50" (without " dollars")Common Patterns
Here are battle-tested patterns for common validation tasks:
Email (simplified)
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$URL
https?://[^\s/$.?#].[^\s]*Phone Number (US)
^(\+1)?[-.\s]?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$IP Address (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])$Hex Color Code
^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$Strong Password
At least 8 characters, with uppercase, lowercase, digit, and special character:
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$Regex Flags
Flags modify how the pattern is applied:
| Flag | Name | Effect |
|---|---|---|
g |
Global | Find all matches, not just the first |
i |
Case-insensitive | a matches both "a" and "A" |
m |
Multiline | ^ and $ match line boundaries |
s |
Dotall | . matches newline characters too |
u |
Unicode | Enable full Unicode support |
Tips for Writing Better Regex
- Start simple — build your pattern incrementally
- Test with edge cases — empty strings, special characters, very long input
- Be specific —
\d{3}is better than\d+when you expect exactly 3 digits - Use non-capturing groups —
(?:...)when you don't need the captured value - Comment complex patterns — use the
xflag or split into named parts - Avoid catastrophic backtracking — patterns like
(a+)+can freeze your program
Test Your Patterns
The best way to learn regex is by experimenting. Our Regex Tester gives you instant visual feedback as you build your patterns — highlighting matches, showing captured groups, and explaining what each part of your pattern does.
Don't memorize — experiment, iterate, and test.