Reference

Regex Cheatsheet

Categorized regex reference with 57+ patterns. Search to find what you need.

Character Classes

PatternDescriptionExample
.Any character except newlinea.c matches "abc"
\dAny digit [0-9]\d+ matches "123"
\DAny non-digit\D+ matches "abc"
\wWord character [a-zA-Z0-9_]\w+ matches "hello_1"
\WNon-word character\W matches "!"
\sWhitespace character\s matches " "
\SNon-whitespace character\S+ matches "hello"
[abc]Character set[aeiou] matches vowels
[^abc]Negated character set[^0-9] matches non-digits
[a-z]Character range[a-z]+ matches "hello"

Anchors

PatternDescriptionExample
^Start of string/line^Hello matches start
$End of string/lineworld$ matches end
\bWord boundary\bword\b exact word
\BNon-word boundary\Bword not at boundary

Quantifiers

PatternDescriptionExample
*Zero or moreab*c matches "ac","abc"
+One or moreab+c matches "abc"
?Zero or onecolou?r matches both
{n}Exactly n times\d{3} matches "123"
{n,}n or more times\d{2,} matches "12","123"
{n,m}Between n and m times\d{2,4} matches "12"-"1234"
*?Lazy zero or more<.*?> non-greedy match
+?Lazy one or more.+? minimal match

Groups & Alternation

PatternDescriptionExample
(abc)Capturing group(\d+) captures digits
(?:abc)Non-capturing group(?:ab)+ matches "abab"
(?<name>)Named capturing group(?<year>\d{4})
a|bAlternation (or)cat|dog matches either
\1Backreference(\w)\1 matches "aa"

Lookahead & Lookbehind

PatternDescriptionExample
(?=abc)Positive lookahead\d(?=px) digit before px
(?!abc)Negative lookahead\d(?!px) digit not before px
(?<=abc)Positive lookbehind(?<=\$)\d+ after $
(?<!abc)Negative lookbehind(?<!\$)\d+ not after $

Flags

PatternDescriptionExample
gGlobal search/pattern/g all matches
iCase insensitive/hello/i matches "HELLO"
mMultiline mode/^line/m each line start
sDotall (. matches \n)/a.b/s spans lines
uUnicode mode/\u{61}/u matches "a"

Common Patterns

PatternDescriptionExample
^[\w.-]+@[\w.-]+\.\w{2,}$Email addressuser@example.com
^https?://[\w.-]+URLhttps://example.com
^\d{1,3}(\.\d{1,3}){3}$IPv4 address192.168.1.1
^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$Hex color#ff0000 or #f00
^\d{4}-\d{2}-\d{2}$Date (YYYY-MM-DD)2024-01-15
^\+?\d{1,3}[-.\s]?\d{3,}Phone number+1-555-1234
^\d{5}(-\d{4})?$US ZIP code12345 or 12345-6789
<[^>]+>HTML tag<div class="x">
\b\d{1,3}(,\d{3})*(\.\d+)?\bNumber with commas1,234,567.89
^(?=.*[A-Z])(?=.*[a-z])(?=.*\d).{8,}$Strong passwordMin 8, upper, lower, digit

Escaping

PatternDescriptionExample
\.Escaped dot (literal .)file\.txt matches "file.txt"
\\Escaped backslashpath\\to matches "path\to"
\(Escaped parenthesisf\(x\) matches "f(x)"
\[Escaped bracket\[0\] matches "[0]"
\{Escaped brace\{a\} matches "{a}"
\*Escaped asterisk2\*3 matches "2*3"
\+Escaped plusC\+\+ matches "C++"
\?Escaped question markwhy\? matches "why?"
\|Escaped pipea\|b matches "a|b"
\^Escaped caret\^start matches "^start"
\$Escaped dollar\$100 matches "$100"