Regex Cheat Sheet

Complete regular expression syntax reference with examples. Click any pattern to copy it, or test it directly in our regex tester.

Character Classes

.Any character except newline
\dDigit (0-9)
Example: \d+ matches "123"
\DNot a digit
Example: \D+ matches "abc"
\wWord character (a-z, A-Z, 0-9, _)
Example: \w+ matches "hello_123"
\WNot a word character
Example: \W+ matches "!@#"
\sWhitespace (space, tab, newline)
Example: \s+ matches " "
\SNot whitespace
Example: \S+ matches "hello"
[abc]Any of a, b, or c
Example: [aeiou] matches vowels
[^abc]Not a, b, or c
Example: [^0-9] matches non-digits
[a-z]Character range (a to z)
Example: [a-zA-Z] matches letters

Quantifiers

*Zero or more times
Example: a* matches "", "a", "aa"
+One or more times
Example: a+ matches "a", "aa", not ""
?Zero or one time (optional)
Example: colou?r matches "color" and "colour"
{n}Exactly n times
Example: \d{3} matches "123"
{n,}n or more times
Example: \d{3,} matches "123", "1234"
{n,m}Between n and m times
Example: \d{2,4} matches "12", "123", "1234"
*?Lazy match (as few as possible)
Example: <.*?> matches "<div>"
+?Lazy match (one or more)
Example: a+? matches "a" in "aaa"

Anchors & Boundaries

^Start of string or line
Example: ^hello matches "hello world"
$End of string or line
Example: world$ matches "hello world"
\bWord boundary
Example: \bword\b matches "word" not "sword"
\BNot a word boundary
Example: \Bword\B matches "sword"
\AStart of string (multiline mode)
Example: \Ahello
\ZEnd of string (multiline mode)
Example: world\Z

Groups & Capture

(abc)Capturing group
Example: (\d{3})-(\d{4}) captures phone
(?:abc)Non-capturing group
Example: (?:Mr|Ms)\.? matches titles
(?<name>abc)Named capturing group
Example: (?<year>\d{4})
\1Backreference to group 1
Example: (\w)\1 matches "aa", "bb"
(?=abc)Positive lookahead
Example: \d(?=px) matches "5" in "5px"
(?!abc)Negative lookahead
Example: \d(?!px) matches digits not before "px"
(?<=abc)Positive lookbehind
Example: (?<=\$)\d+ matches "5" in "$5"
(?<!abc)Negative lookbehind
Example: (?<!\$)\d+ matches digits not after "$"

Flags & Modifiers

gGlobal - find all matches
Example: /a/g matches all "a"s
iCase-insensitive
Example: /hello/i matches "Hello"
mMultiline - ^ and $ match line breaks
Example: /^line/m
sDotAll - . matches newlines
Example: /a.b/s matches "a\nb"
uUnicode - enable full Unicode support
Example: /\u{1F600}/u
ySticky - match from lastIndex
Example: /\d/y

Special Characters

\Escape special character
Example: \. matches literal dot
|Alternation (OR)
Example: cat|dog matches "cat" or "dog"
\nNewline
Example: line1\nline2
\rCarriage return
Example: \r\n
\tTab
Example: name\tvalue
\0Null character
Example: \0
\xhhHex character
Example: \x41 matches "A"
\uhhhhUnicode character
Example: \u0041 matches "A"

Common Patterns

^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$Email address
Example: user@example.com
^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\bURL
Example: https://example.com
^\+?[1-9]\d{1,14}$Phone (international)
Example: +1234567890
^\d{4}-\d{2}-\d{2}$Date (YYYY-MM-DD)
Example: 2025-10-31
^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$Hex color
Example: #FF5733
^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$Password (8+ chars, letter & number)
Example: Pass1234
^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$IPv4 address
Example: 192.168.1.1
^[a-z0-9-]+$URL slug
Example: my-blog-post

Tips & Best Practices

Use Non-Capturing Groups

Use (?:...) when you don't need to capture the match. It's faster and uses less memory.

Test Your Patterns

Always test regex patterns with real data. Use our Regex Tester to verify before deployment.

Avoid Catastrophic Backtracking

Be careful with nested quantifiers like (a+)+. They can cause performance issues.

Use Raw Strings

In many languages, use raw strings (e.g., Python's r"\d+") to avoid double-escaping backslashes.