Regular expressions are one of those tools that feel cryptic until they click — then you use them everywhere. This cheat sheet covers the syntax you'll actually need, with practical examples for common tasks like validation, parsing, and search-and-replace.
Metacharacters
These characters have special meaning in regex. To match them literally, escape with a backslash (\).
| Pattern | Description | Example |
|---|---|---|
. | Any character except newline | a.c → abc, a1c |
^ | Start of string (or line in multiline mode) | ^Hello |
$ | End of string (or line in multiline mode) | world$ |
\ | Escape next character | \. matches literal dot |
| | Alternation (OR) | cat|dog |
() | Capturing group | (abc)+ |
[] | Character class | [aeiou] |
Character Classes
| Pattern | Description |
|---|---|
[abc] | Match a, b, or c |
[^abc] | Match anything except a, b, or c |
[a-z] | Match any lowercase letter |
[A-Z0-9] | Match uppercase letter or digit |
\d | Digit [0-9] |
\D | Non-digit [^0-9] |
\w | Word character [a-zA-Z0-9_] |
\W | Non-word character |
\s | Whitespace (space, tab, newline) |
\S | Non-whitespace |
Quantifiers
Quantifiers specify how many times the preceding element should match. By default they are greedy (match as much as possible). Add ? to make them lazy.
| Pattern | Description | Example |
|---|---|---|
* | 0 or more | ab*c → ac, abc, abbc |
+ | 1 or more | ab+c → abc, abbc |
? | 0 or 1 (optional) | colou?r → color, colour |
{n} | Exactly n times | \d{4} → 2026 |
{n,} | n or more times | \d{2,} → 42, 123 |
{n,m} | Between n and m times | \d{2,4} → 42, 123, 2026 |
*? | 0 or more (lazy) | <.*?> → first tag only |
+? | 1 or more (lazy) |
Anchors & Boundaries
| Pattern | Description | Example |
|---|---|---|
^ | Start of string | ^Start |
$ | End of string | end$ |
\b | Word boundary | \bword\b |
\B | Not a word boundary |
Groups & Backreferences
| Pattern | Description | Example |
|---|---|---|
(abc) | Capturing group | (\d+)-(\d+) |
(?:abc) | Non-capturing group | |
(?<name>abc) | Named capturing group | |
\1 | Backreference to group 1 | (\w+)\s\1 → word word |
(?=abc) | Lookahead (matches if followed by abc) | |
(?!abc) | Negative lookahead | |
(?<=abc) | Lookbehind (matches if preceded by abc) | |
(?<!abc) | Negative lookbehind |
Flags
| Pattern | Description |
|---|---|
g | Global — find all matches, not just the first |
i | Case-insensitive matching |
m | Multiline — ^ and $ match line boundaries |
s | Dotall — . matches newline characters |
u | Unicode — correct handling of Unicode characters |
Common Patterns
Copy-paste ready patterns for everyday validation and parsing tasks:
Email (simplified)
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$URL
https?:\/\/[\w\-]+(\.[\w\-]+)+[\/\w\-._~:?#[\]@!$&'()*+,;=]*IPv4 Address
^(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)$Date (YYYY-MM-DD)
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$Hex Color
^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$Strong Password
At least 8 characters, one uppercase, one lowercase, one digit, one special character:
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$HTML Tags
<([a-zA-Z][a-zA-Z0-9]*)\b[^>]*>(.*?)<\/\1>Tips for Writing Better Regex
- Start simple — get a basic pattern working first, then refine edge cases.
- Use non-capturing groups (
(?:...)) when you don't need the match — it's faster and cleaner. - Be specific —
\d{4}is better than\d+when you know you need exactly 4 digits. - Test with edge cases — empty strings, special characters, and Unicode input.
- Avoid catastrophic backtracking — nested quantifiers like
(a+)+can freeze your program on bad input.
Using regex for server-side validation?
Cloudways provides managed hosting with optimized PHP, Node.js, and Python stacks. Built-in SSL, firewalls, and automated backups so you can focus on your application logic.
Test Your Patterns
Use our Regex Tester to try these patterns with real-time highlighting. Paste any pattern from this guide and test it against your own input — everything runs locally in your browser.