Regex cheat sheet

Last updated April 28, 2026
Short answer
Most regex tutorials throw 200 features at you. In real code you use about 20 of them. This page is those 20, plus the gotchas that catch everyone the first time.

Character classes

PatternMatches
.Any single character except newline (use the s flag if you also want newlines).
\dA digit. By default in ICU/NSRegularExpression this is 0-9 only; turn on Unicode for full digits.
\wA "word" character: letters, digits, underscore.
\sAny whitespace: space, tab, newline.
[abc]One of a, b, or c.
[^abc]Anything that is not a, b, or c.
[a-z]Any lowercase ASCII letter. Use [a-zA-Z] for both cases or just add the i flag.

Quantifiers

PatternMeaning
?0 or 1 (optional).
*0 or more.
+1 or more.
{3}Exactly 3.
{2,5}Between 2 and 5.
{2,}2 or more.
+?, *?Lazy versions. Match as little as possible instead of as much as possible.

Lazy versus greedy is the source of more wrong matches than anything else. If you write ".*" against the string "foo" and "bar", you get the whole thing because * is greedy. ".*?" gives you just "foo".

Anchors and boundaries

PatternMeaning
^Start of the string (or line, with the m flag).
$End of the string (or line, with m).
\bWord boundary. Useful for whole-word matches: \bcat\b matches "cat" but not "category".
\BAnywhere that is not a word boundary.

Groups

Parentheses do two things at once: they group, and they capture.

PatternMeaning
(abc)Group and capture, accessible as $1 or \1.
(?:abc)Group only, no capture. Use this when you just need | or quantifiers and do not want extra capture slots.
(?<name>abc)Named capture. Reference with \k<name> or ${name}.
a|bAlternation: a or b.

Lookahead and lookbehind

These check for context without consuming characters. Read once, you will never forget them.

PatternMeaning
(?=foo)Followed by "foo". a(?=b) matches the "a" in "ab" but not in "ac".
(?!foo)Not followed by "foo".
(?<=foo)Preceded by "foo".
(?<!foo)Not preceded by "foo".

Flags

FlagEffect
iCase insensitive.
mMultiline: ^ and $ match start/end of each line.
sDotall: . also matches newlines.
uUnicode: full Unicode awareness for \d, \w, etc.
xExtended: ignore whitespace and allow comments inside the pattern.

Patterns you will probably need

Email-ish (catches almost anything that looks like an email, not RFC-strict):

[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}

URL:

https?://[^\s]+

UK / US phone, very loose:

\+?\d[\d\s().-]{7,}\d

ISO date YYYY-MM-DD:

\b\d{4}-\d{2}-\d{2}\b

IPv4 (loose):

\b(?:\d{1,3}\.){3}\d{1,3}\b

Hex color:

#?[0-9a-fA-F]{6}\b

Trim multiple blank lines into one (replace mode, replacement = \n\n):

(\r?\n){3,}

The escape rules

Most regex flavours treat these as special characters and require a backslash if you want the literal version: . ^ $ * + ? ( ) [ ] { } | \. Inside a character class ([ ]), most of these are already literal, but ] \ and - (in the middle of a class) still need attention.

When you copy a regex into a JSON string or a JavaScript source file, every backslash has to be doubled. \d in your pattern becomes "\\d" in JSON. This is the second-most common reason a working regex stops working when pasted somewhere.

Regex Tool: Pattern Editor app icon

Regex Tool: Pattern Editor

Build, test, and debug regular expressions on iPhone, iPad, and Mac with live match highlighting. · iPhone, iPad & Mac

Related entries