Technology
Regular Expressions Cheat Sheet: Regex Tutorial and Reference
8 min read · Updated 2025
Regular expressions (regex) are patterns used to match, search, and manipulate text. They are available in virtually every programming language and text editor. This guide covers everything from basic syntax to practical patterns with a complete reference cheat sheet.
Test all the patterns from this guide instantly with BrainBoost's Regex Tester.
Character Classes
| Pattern | Matches | Example |
|---|---|---|
| . | Any character except newline | c.t matches "cat", "cut", "c4t" |
| \d | Any digit [0-9] | \d+ matches "42", "007" |
| \D | Any non-digit | \D+ matches "hello", "abc" |
| \w | Word character [a-zA-Z0-9_] | \w+ matches "hello_world" |
| \W | Non-word character | \W+ matches "!@#" |
| \s | Whitespace (space, tab, newline) | \s+ matches spaces |
| \S | Non-whitespace | \S+ matches "hello" |
| [abc] | Any of a, b, or c | [aeiou] matches any vowel |
| [^abc] | Any character NOT a, b, or c | [^aeiou] matches consonants |
| [a-z] | Any lowercase letter | [a-f] matches a through f |
Quantifiers
| Quantifier | Meaning | Example |
|---|---|---|
| * | 0 or more | bo* matches "b", "bo", "boo" |
| + | 1 or more | bo+ matches "bo", "boo" (not "b") |
| ? | 0 or 1 (optional) | colou?r matches "color" and "colour" |
| {n} | Exactly n times | \d{4} matches exactly 4 digits |
| {n,} | n or more times | \d{2,} matches 2+ digits |
| {n,m} | Between n and m times | \d{2,4} matches 2 to 4 digits |
| *? | Lazy: 0 or more (minimal) | <.*?> matches one HTML tag at a time |
Anchors and Boundaries
| Pattern | Matches |
|---|---|
| ^ | Start of string (or line in multiline mode) |
| $ | End of string (or line in multiline mode) |
| \b | Word boundary |
| \B | Non-word boundary |
Groups and Alternation
| Pattern | Meaning |
|---|---|
| (abc) | Capture group — match and remember "abc" |
| (?:abc) | Non-capture group — match but don't remember |
| a|b | Alternation — match "a" or "b" |
| \1 | Backreference to capture group 1 |
Common Real-World Patterns
| Pattern | What it matches |
|---|---|
[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,} | Email address |
https?:\/\/[\w\-]+(\.[\w\-]+)+[\/\w\-?=%]* | URL (http/https) |
\d{4}-\d{2}-\d{2} | Date (YYYY-MM-DD) |
\b(?:\d{1,3}\.){3}\d{1,3}\b | IPv4 address |
#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})\b | Hex color code |
^\+?[\d\s\-\(\)]{7,15}$ | Phone number |
^(?=.*[A-Z])(?=.*\d).{8,}$ | Password (8+ chars, 1 uppercase, 1 digit) |
Frequently Asked Questions
A regular expression (regex) is a sequence of characters that defines a search pattern. Used for finding, matching, and manipulating text in programming, text editors, and command-line tools.
The dot (.) matches any single character except a newline. The asterisk (*) means 'zero or more of the preceding element'. Together .* matches zero or more of any character.
g (global) — find all matches. i (case-insensitive) — match regardless of case. m (multiline) — ^ and $ match start/end of each line. s (dotall) — . matches newlines too.