Complete regular expression reference — character classes, quantifiers, assertions, groups, and ready-to-use patterns. Bookmark this page.
Try the AI Regex Generator →| Pattern | Description | Example |
|---|---|---|
. | Any character except newline | h.t matches "hat", "hot", "h9t" |
\d | Any digit (0-9) | \d{3} matches "123", "456" |
\D | Any non-digit | \D+ matches "abc", "!@#" |
\w | Word character (a-z, A-Z, 0-9, _) | \w+ matches "hello_42" |
\W | Non-word character | \W matches "!", " ", "@" |
\s | Whitespace (space, tab, newline) | \s+ matches " \t\n" |
\S | Non-whitespace | \S+ matches "hello" |
[abc] | Any one of a, b, or c | [aeiou] matches any vowel |
[^abc] | Not a, b, or c | [^0-9] matches non-digits |
[a-z] | Any character in range | [A-Za-z] matches any letter |
| Pattern | Description | Example |
|---|---|---|
* | Zero or more | ab*c matches "ac", "abc", "abbc" |
+ | One or more | ab+c matches "abc", "abbc" (not "ac") |
? | Zero or one (optional) | colou?r matches "color", "colour" |
{n} | Exactly n times | \d{4} matches "2026" |
{n,} | n or more times | \w{3,} matches words ≥3 chars |
{n,m} | Between n and m times | \d{2,4} matches "12", "123", "1234" |
*? | Zero or more (lazy) | <.*?> matches shortest tag |
+? | One or more (lazy) | ".+?" matches shortest quoted string |
* and + are greedy — they match as much as possible. Add ? after them to make them lazy (match as little as possible). This matters when parsing HTML, JSON, or any nested structure.
| Pattern | Description | Example |
|---|---|---|
^ | Start of string (or line with m flag) | ^Hello matches "Hello world" |
$ | End of string (or line with m flag) | world$ matches "Hello world" |
\b | Word boundary | \bcat\b matches "cat" (not "category") |
\B | Not a word boundary | \Bcat matches "concatenate" |
| Pattern | Description | Example |
|---|---|---|
(abc) | Capturing group | (\d{3})-(\d{4}) captures area code + number |
(?:abc) | Non-capturing group | (?:http|https):// groups without capturing |
(?<name>abc) | Named capture group | (?<year>\d{4}) captures as "year" |
\1 | Backreference to group 1 | (\w+)\s\1 matches "the the" |
a|b | Alternation (a or b) | cat|dog matches "cat" or "dog" |
| Pattern | Description | Example |
|---|---|---|
(?=abc) | Positive lookahead | \d+(?= dollars) matches "100" in "100 dollars" |
(?!abc) | Negative lookahead | \d+(?! dollars) matches "100" in "100 euros" |
(?<=abc) | Positive lookbehind | (?<=\$)\d+ matches "50" in "$50" |
(?<!abc) | Negative lookbehind | (?<!\$)\d+ matches "50" in "€50" |
| Flag | Name | Effect |
|---|---|---|
g | Global | Match all occurrences, not just the first |
i | Case-insensitive | /hello/i matches "Hello", "HELLO" |
m | Multiline | ^ and $ match start/end of each line |
s | Dotall | . matches newline characters too |
u | Unicode | Enable full Unicode matching (emoji, CJK) |
These characters have special meaning in regex. To match them literally, escape with backslash \:
. * + ? ^ $ { } [ ] ( ) | \ /
Example: to match a literal period, use \. instead of .
Copy-paste ready patterns for everyday use. Test them in our Regex Generator.
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
https?://[^\s/$.?#].[^\s]*
\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}
\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])
#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})\b
<([a-z]+)([^<]*)(?:>(.*?)<\/\1>|\/>)
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%]).{8,}$
^[a-zA-Z0-9_-]{3,20}$
\b\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{4}\b
Begin with a loose pattern that matches too much, then tighten it. It's easier to debug a pattern that matches too many things than one that matches nothing.
If you don't need the captured text, use (?:...) instead of (...). This saves memory when processing large files.
Use ^ and $ to anchor your pattern. ^\d{3}$ is much faster than \d{3} because the engine knows exactly where to look.
Nested quantifiers like (a+)+ can cause catastrophic backtracking (exponential time). Avoid patterns where a quantified group contains another quantifier matching the same characters.
Always test with empty strings, strings with only whitespace, very long inputs, and Unicode characters. Our Regex Generator makes this easy with live match highlighting.
Describe what you want to match in plain English and our AI generates the pattern for you.
Open Regex Generator →