Regex Cheat Sheet

The regex syntax you will actually reach for, plus copy-ready patterns for email, URLs, dates, and phone numbers. Print it, and test every pattern before you trust it with real data.

Regular expressions (regex) describe a pattern of text, and a regex engine finds every match. The same core syntax works in JavaScript, Python, and most text editors, but a few features differ by flavor, so notes below flag the exceptions. Test every pattern before you use it in real data.

Character classes

PatternMatches
.Any single character except a line break
[abc]Any one of a, b, or c
[^abc]Any one character except a, b, or c
[a-z]Any one lowercase letter; [A-Z] is uppercase, [0-9] is a digit
\dAny digit, same as [0-9]
\DAny character that is not a digit
\wA word character: a letter, digit, or underscore
\WAny character that is not a word character
\sAny whitespace: space, tab, or line break
\SAny character that is not whitespace
\bA word boundary, the edge between a word character and a non-word character

Quantifiers

PatternMatches
*Zero or more of the previous item (greedy)
+One or more of the previous item (greedy)
?Zero or one of the previous item
{n}Exactly n of the previous item
{n,}n or more of the previous item
{n,m}Between n and m of the previous item
*? +? ??The lazy versions: match as little as possible instead of as much as possible

Greedy is the default: .+ on <b>one</b> and <b>two</b> matches the whole line, while .+? stops at the first closing tag.

Anchors

PatternMatches
^The start of the string (or the start of a line with the multiline flag)
$The end of the string (or the end of a line with the multiline flag)
\AThe start of the string, always (not supported in JavaScript)
\zThe end of the string, always (not supported in JavaScript)
\bword\bThe whole word word, not part of a longer word

Groups and alternation

PatternMatches
(abc)A capturing group: matches abc and remembers it for later use
(?:abc)A non-capturing group: groups without remembering
a|bEither a or b
(?<year>\d{4})A named group called year (JavaScript, Python, and .NET; not in older flavors)
(?=abc)A lookahead: matches only if abc follows, without consuming it
(?!abc)A negative lookahead: matches only if abc does not follow
(?<=abc)A lookbehind: matches only if abc comes before (not in older JavaScript)

Escapes

PatternMatches
\. \* \+ \? \( \) \[ \] \{ \} \^ \$ \|The literal character, not its special meaning
\\A literal backslash
\nA line break
\tA tab

Common recipes

These are starting points for everyday validation, not perfect checkers. Each one is written to match the whole string, so keep the ^ and $ anchors.

What it checksPatternNotes
Email address^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$Covers everyday addresses. Real email rules allow rare edge cases this rejects, so for strict checking, send a confirmation email instead.
Web URL^https?://[^\s]+$Checks the shape only. It accepts any address starting with http:// or https:// with no spaces.
Date, YYYY-MM-DD^\d{4}-\d{2}-\d{2}$Checks the shape only. It would accept 2026-13-45, which is not a real date, so validate the numbers separately.
US phone number^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$Matches (555) 123-4567, 555-123-4567, 555.123.4567, and 5551234567.
IPv4 address^(?:\d{1,3}\.){3}\d{1,3}$Checks the shape only. It accepts 999.999.999.999, so check each number is 0 to 255 separately.
Hex color^#(?:[0-9a-fA-F]{3}){1,2}$Matches #fff and #ffffff, in upper or lower case.
Username, 3 to 16 characters^[a-zA-Z0-9_]{3,16}$Letters, digits, and underscores only.
Collapse repeated spaces\s+ replaced with a single spaceUseful for cleaning pasted text.
Regex flavors differ. JavaScript, Python, and most editors share the core above, but lookbehind, named groups, and \A and \z are not available everywhere. When a pattern works in one tool and fails in another, the flavor is the first thing to check.

Common questions

Why does my regex match too much text? Quantifiers are greedy by default: .+ takes as much as it can. Add a question mark to make it lazy, so .+? stops at the first possible end, or replace the dot with a more specific class, such as [^<]+ to stop at a closing tag.

Do I need to escape a dot in a pattern? Yes, when you mean a literal dot. A bare . matches any character, so example.com as a pattern would also match exampleXcom. Write example\.com to match the dot itself.

Is the email pattern on this page perfect? No. The official email rules allow rare addresses that the pattern rejects, and it accepts some that no mail server would deliver to. It covers the addresses people actually type, and for anything stricter, the reliable check is sending a confirmation email.

Related

Regex Tester · Keyboard Shortcuts Cheat Sheet · Windows Command Line Cheat Sheet

Was this helpful?