Regex Cheat Sheet: Complete Regular Expression Reference
A complete regex quick reference: character classes, quantifiers, anchors, groups, lookaheads, flags, and 15 common ready-to-use patterns for emails, URLs, dates, and more.
Advertisement
Regular expressions (regex) are patterns used to match, search, and manipulate text. The syntax is compact but can feel cryptic until you've internalized the building blocks. This cheat sheet is your quick reference for everything from basic character matching to advanced lookaheads, with 15 ready-to-use patterns for common validation tasks.
Test your regex
Use our free Regex Tester to build and test regular expressions against sample text. It highlights all matches, shows groups, and flags errors in real time.
Test Regex in Your Browser — Free
Build, test, and debug regular expressions with live match highlighting.
Character Classes
Regex character classes
| Pattern | Matches | Example |
|---|---|---|
| . | Any character except newline | a.c matches 'abc', 'a1c', 'a c' |
| \d | Digit [0-9] | \d+ matches '42', '100' |
| \D | Non-digit [^0-9] | \D+ matches 'abc', 'hello' |
| \w | Word char [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 non-space runs |
| [abc] | Any of: a, b, or c | [aeiou] matches any vowel |
| [^abc] | NOT a, b, or c | [^0-9] matches non-digits |
| [a-z] | Character range a through z | [a-zA-Z] matches any letter |
| [\w\s] | Word chars or whitespace | Matches words with spaces |
Advertisement
Quantifiers
Regex quantifiers
| Quantifier | Meaning | Example |
|---|---|---|
| * | 0 or more (greedy) | a* matches '', 'a', 'aaa' |
| + | 1 or more (greedy) | a+ matches 'a', 'aaa' (not '') |
| ? | 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{3,} matches 3+ digits |
| {n,m} | Between n and m times | \d{2,4} matches 2–4 digits |
| *? | 0 or more (lazy) | a*? matches minimal 'a's |
| +? | 1 or more (lazy) | a+? matches minimal 'a's |
| ?? | 0 or 1 (lazy) | a?? prefers 0 matches |
Anchors
Regex anchors
| Anchor | Matches | 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 'concatenate' |
| \B | Non-word boundary | \Bcat\B matches 'cat' in 'concatenate' |
| \A | Start of string (Python/Java) | Absolute string start |
| \Z | End of string (Python/Java) | Absolute string end |
Groups and Alternation
Regex groups and alternation
| Syntax | Meaning | Example |
|---|---|---|
| (abc) | Capturing group — stores the match | (\d+) captures number |
| (?:abc) | Non-capturing group — groups without storing | (?:Mr|Mrs)\. |
| (?P<name>abc) | Named capturing group (Python/PCRE) | (?P<year>\d{4}) |
| (?<name>abc) | Named capturing group (JS/PCRE2) | (?<year>\d{4}) |
| a|b | Alternation: a OR b | cat|dog matches 'cat' or 'dog' |
| \1 | Backreference to group 1 | (\w+) \1 matches 'hello hello' |
Advertisement
Lookaheads and Lookbehinds
Regex lookaheads and lookbehinds
| Syntax | Meaning | Example |
|---|---|---|
| (?=abc) | Positive lookahead: followed by abc | \d+(?= dollars) matches numbers before 'dollars' |
| (?!abc) | Negative lookahead: NOT followed by abc | \d+(?! dollars) excludes dollar amounts |
| (?<=abc) | Positive lookbehind: preceded by abc | (?<=\$)\d+ matches digits after '$' |
| (?<!abc) | Negative lookbehind: NOT preceded by abc | (?<!\$)\d+ excludes dollar amounts |
Flags / Modifiers
Common regex flags
| Flag | Name | Effect |
|---|---|---|
| i | Case-insensitive | /hello/i matches 'Hello', 'HELLO', 'hello' |
| g | Global | Find all matches (not just first) |
| m | Multiline | ^ and $ match start/end of each line |
| s | Dotall (singleline) | . matches newlines as well |
| x | Extended (verbose) | Allows whitespace and comments in pattern |
| u | Unicode | Enable Unicode matching (/\u{1F600}/u) |
Common Regex Patterns
Ready-to-use regex patterns
| Pattern | Matches | Regex |
|---|---|---|
| Basic email format | [a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,} | |
| URL (http/https) | Web URLs | https?:\/\/[\w\-]+(\.[\w\-]+)+[\/\w\-\._~:/?#[\]@!$&'()*+,;=]* |
| Phone (US) | US phone number | ^(\+1\s?)?\(?\d{3}\)?[\s.\-]?\d{3}[\s.\-]?\d{4}$ |
| Date (YYYY-MM-DD) | ISO date | ^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$ |
| IPv4 address | IP address | ^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$ |
| Hex colour | #RGB or #RRGGBB | ^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$ |
| UUID v4 | UUID format | [0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12} |
| Postal code (UK) | UK postcode | ^[A-Z]{1,2}\d[A-Z\d]?\s?\d[A-Z]{2}$ |
| Strong password | 8+ chars, mixed | ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[\W_]).{8,}$ |
| Credit card (Visa) | Visa card number | ^4[0-9]{12}(?:[0-9]{3})?$ |
| HTML tag | Any HTML tag | <([a-zA-Z][a-zA-Z0-9]*)\b[^>]*>([\s\S]*?)<\/\1> |
| Slug | URL-safe slug | ^[a-z0-9]+(?:-[a-z0-9]+)*$ |
| Username | Alphanumeric + _- | ^[a-zA-Z0-9_\-]{3,16}$ |
| Whitespace-only | Blank or whitespace | ^\s*$ |
| Non-empty string | At least one char | \S+ |
Regex in Different Languages
Regex usage in common programming languages
| Language | Test a match | Find all matches |
|---|---|---|
| JavaScript | /pattern/flags.test(str) | str.match(/pattern/g) or [...str.matchAll(/pattern/g)] |
| Python | re.search(pattern, str) | re.findall(pattern, str) |
| Java | Pattern.compile(p).matcher(s).matches() | Matcher.find() in a loop |
| PHP | preg_match('/pattern/', $str) | preg_match_all('/pattern/', $str, $matches) |
| Go | regexp.MustCompile(p).MatchString(s) | regexp.MustCompile(p).FindAllString(s, -1) |
| C# / .NET | Regex.IsMatch(str, pattern) | Regex.Matches(str, pattern) |
| Ruby | str.match?(/pattern/) | str.scan(/pattern/) |
Test Your Regex Patterns Free
Build and debug regex with live match highlighting. Works for JavaScript and PCRE syntax.
Advertisement
Frequently Asked Questions
What does .* mean in regex?
. means any character (except newline by default). * means 0 or more of the preceding element. Together .* means 'zero or more of any character' — it matches everything on a line. .+ requires at least one character.
What is the difference between * and + in regex?
* means 0 or more occurrences (it's optional). + means 1 or more occurrences (at least one required). \d* matches '' or '42' or '1234'. \d+ only matches '42' or '1234', not ''.
How do I match an exact string in regex?
For an exact full-string match, anchor the pattern with ^ at the start and $ at the end: ^Hello world$. Without anchors, the pattern can match a substring anywhere in the text.
How do I make a regex case-insensitive?
Add the 'i' flag: /hello/i matches 'hello', 'Hello', 'HELLO'. In Python: re.compile(pattern, re.IGNORECASE). In Java: Pattern.compile(pattern, Pattern.CASE_INSENSITIVE).
What is a capturing group in regex?
A capturing group ( ) stores the text matched by the subpattern inside the parentheses. In JavaScript, match[1] gives you the first group. Use (?:) for non-capturing groups if you want grouping without storage overhead.
How do I escape special characters in regex?
Prepend a backslash \. These regex metacharacters must be escaped: . * + ? ^ $ { } [ ] | ( ) \. To match a literal dot, use \. To match a literal parenthesis, use \( and \).