Regex Cheat Sheet — Regular Expression Reference

Comprehensive regular expression reference with syntax, descriptions, and examples. Covers all major regex flavors (JavaScript, Python, PCRE, Java).

Try these patterns live with our Regex Tester or debug step-by-step with the Regex Debugger.

Character Classes

PatternDescriptionExample
.Any character except newlinea.c matches abc, a1c, a-c
\dAny digit (0-9)\d{3} matches 123, 456
\DAny non-digit character\D+ matches abc in abc123
\wWord character (a-z, A-Z, 0-9, _)\w+ matches hello_42
\WNon-word character\W matches @ in user@host
\sWhitespace (space, tab, newline)a\sb matches a b
\SNon-whitespace character\S+ matches hello
[abc]Any one of a, b, or c[aeiou] matches e in hello
[^abc]Any character except a, b, or c[^0-9] matches a in a1
[a-z]Any character in range a to z[A-Za-z] matches any letter
[a-zA-Z0-9]Any alphanumeric characterEquivalent to \w without underscore
[[:alpha:]]POSIX: any letter[[:alpha:]]+ matches hello
[[:digit:]]POSIX: any digitEquivalent to \d or [0-9]

Quantifiers

PatternDescriptionExample
*0 or more (greedy)ab*c matches ac, abc, abbc
+1 or more (greedy)ab+c matches abc, abbc but not ac
?0 or 1 (optional)colou?r matches color and colour
{n}Exactly n times\d{4} matches 2024
{n,}n or more times\d{2,} matches 42, 123, 9999
{n,m}Between n and m times\d{2,4} matches 42, 123, 1234
*?0 or more (lazy)<.*?> matches <b> in <b>bold</b>
+?1 or more (lazy)\d+? matches 1 in 123
??0 or 1 (lazy)\d?? prefers matching nothing
*+0 or more (possessive, PCRE)\d*+ no backtracking allowed
++1 or more (possessive, PCRE)\w++ no backtracking allowed

Anchors & Boundaries

PatternDescriptionExample
^Start of string (or line with m flag)^Hello matches Hello at the start
$End of string (or line with m flag)world$ matches world at the end
\bWord boundary\bcat\b matches cat but not catch
\BNon-word boundary\Bcat\B matches cat in concatenate
\AStart of string (never affected by m flag)\AStart matches only at absolute start
\ZEnd of string (before final newline)end\Z matches at string end
\zAbsolute end of stringend\z matches at the very end

Groups & Backreferences

PatternDescriptionExample
(abc)Capturing group(ha)+ matches haha
(?:abc)Non-capturing group(?:ha)+ matches haha without capture
(?<name>abc)Named capturing group(?<year>\d{4}) captures and names it "year"
\1Backreference to group 1(["'])(.*?)\1 matches matching quotes
\k<name>Backreference to named group(?<q>["'])(.*?)\k<q>
(a|b)Alternation (a or b)(cat|dog) matches cat or dog
(?P<name>abc)Named group (Python syntax)(?P<user>\w+) Python-style named group
(?P=name)Backreference (Python syntax)(?P<q>["']).*?(?P=q)

Lookahead & Lookbehind

PatternDescriptionExample
(?=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 EUR50 but not $50

Lookbehind requires fixed-width patterns in most engines. JavaScript supports variable-length lookbehind since ES2018.

Flags / Modifiers

FlagDescriptionExample
gGlobal — find all matches, not just the first/\d+/g finds all numbers in a string
iCase-insensitive matching/hello/i matches Hello, HELLO
mMultiline — ^ and $ match line boundaries/^start/m matches start on any line
sDotall — . matches newline characters too/a.b/s matches a\nb
uUnicode — enables full Unicode matching/\u{1F600}/u matches emoji
xExtended — ignore whitespace, allow commentsAllows # comments in pattern (PCRE, Python)
ySticky — match only at lastIndex (JS)Anchors match to specific position in string
dIndices — generate match indices (JS ES2022)Provides start/end positions for captures

Escape Sequences

PatternDescriptionExample
\\Literal backslashC:\\Users matches C:\Users
\.Literal dot3\.14 matches 3.14 but not 3X14
\*Literal asterisk5\*3 matches 5*3
\+Literal plus1\+1 matches 1+1
\?Literal question markwhy\? matches why?
\^Literal caret\^_\^ matches ^_^
\$Literal dollar sign\$100 matches $100
\{ \}Literal braces\{key\} matches {key}
\[ \]Literal brackets\[0\] matches [0]
\( \)Literal parentheses\(1\) matches (1)
\|Literal pipea\|b matches a|b
\tTab charactercol1\tcol2 matches tab-separated values
\nNewline (line feed)line1\nline2 matches across lines
\rCarriage return\r\n matches Windows line endings
\0Null characterMatches the NUL byte
\xhhHex character code\x41 matches A
\uhhhhUnicode character (JS)\u00E9 matches é
\p{Category}Unicode category (with u flag)\p{Emoji} matches emoji characters

Common Patterns — Ready to Use

Copy-paste regex patterns for common validation tasks. Test them with our Regex Tester.

Use CasePatternMatches
Email Address[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}user@example.com
URL (HTTP/HTTPS)https?:\/\/[^\s/$.?#].[^\s]*https://example.com/path
IPv4 Address\b(?:\d{1,3}\.){3}\d{1,3}\b192.168.1.1
IPv6 Address([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}2001:0db8:85a3:0000:0000:8a2e:0370:7334
Phone (US)\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}(555) 123-4567, 555.123.4567
Phone (Intl)\+?\d{1,3}[-.\s]?\(?\d{1,4}\)?[-.\s]?\d{1,4}[-.\s]?\d{1,9}+44 20 7946 0958
Date (YYYY-MM-DD)\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])2024-12-25
Date (MM/DD/YYYY)(0[1-9]|1[0-2])\/(0[1-9]|[12]\d|3[01])\/\d{4}12/25/2024
Time (24h)([01]\d|2[0-3]):[0-5]\d(:[0-5]\d)?14:30, 23:59:59
Credit Card\b\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{4}\b4111 1111 1111 1111
Hex Color#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})\b#fff, #3b82f6
Username^[a-zA-Z0-9_-]{3,20}$john_doe42
Strong Password^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[\W_]).{8,}$Min 8 chars: upper, lower, digit, special
Slug (URL-safe)^[a-z0-9]+(?:-[a-z0-9]+)*$my-blog-post-123
HTML Tag<([a-zA-Z][a-zA-Z0-9]*)\b[^>]*>(.*?)<\/\1><div>content</div>
Whitespace Trim^\s+|\s+$Leading/trailing whitespace
Duplicate Words\b(\w+)\s+\1\bthe the (repeated words)
MAC Address([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}00:1B:44:11:3A:B7
SSN (US)\b\d{3}-\d{2}-\d{4}\b123-45-6789
ZIP Code (US)\b\d{5}(-\d{4})?\b90210, 90210-1234

Quick Reference — Metacharacters

Characters that must be escapedDescription
. * + ? ^ $ { } [ ] ( ) | \These are metacharacters with special meaning in regex. To match them literally, prefix with a backslash: \. \* \+ etc.
] ^ - \Inside a character class [...], only these characters need escaping (and - only when not at the start or end).

Frequently Asked Questions

What is a regular expression (regex)?
A regular expression (regex or regexp) is a sequence of characters that defines a search pattern. It is used for string matching, searching, and text manipulation in programming languages, text editors, and command-line tools. Regex is supported in virtually every programming language including JavaScript, Python, Java, Go, PHP, and Ruby.
What is the difference between greedy and lazy quantifiers?
Greedy quantifiers (*, +, {n,m}) match as much text as possible while still allowing the overall pattern to succeed. Lazy (non-greedy) quantifiers (*?, +?, {n,m}?) match as little text as possible. For example, given the string <b>bold</b>, the greedy pattern <.*> matches the entire string, while the lazy pattern <.*?> matches only <b>.
What are lookahead and lookbehind assertions?
Lookahead and lookbehind are zero-width assertions that match a position in the string without consuming characters. A positive lookahead (?=...) asserts that what follows matches the pattern. A negative lookahead (?!...) asserts that what follows does not match. Similarly, positive lookbehind (?<=...) and negative lookbehind (?<!...) check what precedes the current position. They are useful for conditional matching without including the checked text in the result.
How do I match special characters literally in regex?
To match regex special characters (metacharacters) literally, you need to escape them with a backslash (\). The characters that need escaping are: . * + ? ^ $ { } [ ] ( ) | \. For example, to match a literal dot, use \. instead of just .. Inside a character class [...], most special characters lose their meaning except for ], \, ^, and - which still need escaping in certain positions.