ยท Updated May 3, 2026

Regex Cheat Sheet

A compact regex cheat sheet with the most-used patterns. For a step-by-step tutorial, see Regular Expressions Cheat Sheet with Examples. To test patterns interactively, use our free Regex Tester.

Character Classes

PatternDescriptionExampleMatches
.Any character except newlinea.cabc, a1c, a-c
\dDigit (0โ€“9)\d{3}123, 456
\DNon-digit\D+abc, ---
\wWord character (aโ€“z, Aโ€“Z, 0โ€“9, _)\w+hello, var_1
\WNon-word character\W@, #, space
\sWhitespace (space, tab, newline)a\sba b
\SNon-whitespace\S+hello
[abc]Any of a, b, or c[aeiou]a, e, i
[^abc]Not a, b, or c[^0-9]a, Z, -
[a-z]Range: a through z[A-Za-z]a, Z

Quantifiers

PatternDescriptionExampleMatches
*0 or moreab*cac, abc, abbc
+1 or moreab+cabc, abbc
?0 or 1colou?rcolor, colour
{n}Exactly n\d{4}2026
{n,}n or more\d{2,}12, 123, 1234
{n,m}Between n and m\d{2,4}12, 123, 1234
*?Lazy * (as few as possible)".*?"โ€aโ€ in โ€œaโ€ โ€œbโ€
+?Lazy +".+?"โ€aโ€ in โ€œaโ€ โ€œbโ€

Anchors

PatternDescriptionExample
^Start of string (or line with m flag)^Hello
$End of string (or line with m flag)world$
\bWord boundary\bword\b
\BNon-word boundary\Bend

Groups and References

PatternDescriptionExampleMatches
(abc)Capturing group(ha)+ha, haha
(?:abc)Non-capturing group(?:ha)+ha, haha
(?<name>abc)Named group(?<year>\d{4})2026
\1Back-reference to group 1(a)\1aa
(?P=name)Named back-reference (Python)โ€”โ€”

Lookaround

PatternDescriptionExampleMatches
(?=abc)Lookahead: followed by abc\d(?=px)5 in 5px
(?!abc)Negative lookahead: not followed by abc\d(?!px)5 in 5em
(?<=abc)Lookbehind: preceded by abc(?<=\$)\d+100 in $100
(?<!abc)Negative lookbehind: not preceded by abc(?<!\$)\d+100 in โ‚ฌ100

Flags

FlagNameDescription
gGlobalFind all matches, not just the first
iCase-insensitiveA matches a
mMultiline^ and $ match line boundaries
sDotAll. matches newlines too
uUnicodeEnable Unicode matching
xExtendedIgnore whitespace, allow comments

Common Patterns

PurposePattern
Email (simple)[\w.-]+@[\w.-]+\.\w{2,}
URLhttps?://[^\s/$.?#].[^\s]*
IPv4 address\b\d{1,3}(\.\d{1,3}){3}\b
Hex color#[0-9a-fA-F]{3,8}
ISO date (YYYY-MM-DD)\d{4}-\d{2}-\d{2}
Phone (US)\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}
HTML tag<([a-z][\w-]*)\b[^>]*>.*?</\1>
Integer-?\d+
Float-?\d+\.\d+
Slug[a-z0-9]+(?:-[a-z0-9]+)*

Special Characters (Need Escaping)

These characters have special meaning and must be escaped with \ to match literally:

.  *  +  ?  ^  $  |  \  (  )  [  ]  {  }

For example, to match a literal dot: \.
To match a literal dollar sign: \$

Quick Tips

  • Test before you commit: open the Regex Tester in a new tab and verify matches against real sample text before pasting the pattern into your codebase.
  • Use anchors for full-string validation: wrap your pattern in ^...$ so it only matches when the entire string conforms.
  • Prefer character classes over .: [^"]+ is faster and clearer than .+? when matching content between delimiters.
  • Watch out for catastrophic backtracking: nested quantifiers like (a+)+ can hang on non-matching input. Avoid them or use atomic groups where supported.
  • Different engines, different syntax: JavaScript, Python, Java, and POSIX regex have subtle differences. The patterns here target JavaScript / PCRE-compatible engines.
regex reference programming

Related Tools

More Cheat Sheets