Regex Cheatsheet
Quick reference for regular expression syntax. Search or browse by category and click any token to copy.
Character Classes(10)
.Any character
Matches any single character except newline
Example
a.c matches abc\dDigit
Matches any digit (0-9)
Example
\d\d matches 42\wWord character
Matches letter, digit, or underscore
Example
\w+ matches hello_123\sWhitespace
Matches space, tab, newline
Example
a\sb matches "a b"\DNon-digit
Opposite of \d
Example
\D matches a\WNon-word
Opposite of \w
Example
\W matches !\SNon-whitespace
Opposite of \s
Example
\S matches a[abc]Character set
Matches any of a, b, c
Example
[aeiou] matches e[^abc]Negated set
Matches any character NOT a, b, c
Example
[^aeiou] matches s[a-z]Range
Matches characters a through z
Example
[a-z]+ matches helloAnchors(4)
^Start of string
Anchor that matches at the start of the string
Example
^Hello matches at start$End of string
Anchor that matches at the end of the string
Example
world$ matches at end\bWord boundary
Anchor that matches at a word boundary
Example
\bword\b matches whole word\BNon-word boundary
Anchor that matches where there is NOT a word boundary
Example
\Bend matches "end" within "weekend"Quantifiers(8)
*Zero or more
Matches the preceding token zero or more times
Example
ab*c matches ac, abc, abbc+One or more
Matches the preceding token one or more times
Example
ab+c matches abc, abbc?Zero or one
Matches the preceding token zero or one time (optional)
Example
colou?r matches color, colour{n}Exactly n
Matches the preceding token exactly n times
Example
a{3} matches aaa{n,}At least n
Matches the preceding token n or more times
Example
a{2,} matches aa, aaa{n,m}Between n and m
Matches the preceding token between n and m times
Example
a{2,4} matches aa to aaaa*?Lazy zero or more
Matches as few as possible of the preceding token
Example
a.*?b matches "axxb" stops at first b+?Lazy one or more
Matches as few as possible (at least one)
Example
a.+?b matches "axxb" stops at first bGroups & Lookarounds(6)
(abc)Capturing group
Groups a pattern and captures the matched text
Example
(ab)+ matches abab(?:abc)Non-capturing group
Groups a pattern without capturing the matched text
Example
(?:ab)+ matches abab(?=abc)Positive lookahead
Asserts that what follows matches without consuming
Example
a(?=b) matches a only if followed by b(?!abc)Negative lookahead
Asserts that what follows does NOT match
Example
a(?!b) matches a only if NOT followed by b(?<=abc)Positive lookbehind
Asserts that what precedes matches without consuming
Example
(?<=a)b matches b only if preceded by a(?<!abc)Negative lookbehind
Asserts that what precedes does NOT match
Example
(?<!a)b matches b only if NOT preceded by aAlternation(1)
|Or
Acts as an OR operator between alternatives
Example
cat|dog matches cat or dogEscaped Characters(7)
\nNewline
Matches a line feed character
Example
a\nb matches "a" then new line "b"\rCarriage return
Matches a carriage return character
Example
\r\n matches CRLF line ending\tTab
Matches a horizontal tab character
Example
a\tb matches "a<TAB>b"\\Literal backslash
Matches a literal backslash character
Example
a\\b matches a\b\.Literal dot
Matches a literal period/dot character
Example
example\.com matches example.com\xHHHex character
Matches the character with the specified hex code
Example
\x41 matches A\uHHHHUnicode character
Matches the Unicode character with the specified hex code
Example
\u00A9 matches ©