Regular Expressions Pocket Guide

A quick reference for Regular Expressions. Write powerful patterns to match, search, and replace text.

Character Classes

Key / CodeDescription
.Any character (except newline)
\dDigit (0-9)
\wWord character (a-z, A-Z, 0-9, _)
\sWhitespace (space, tab, newline)
[abc]Any of a, b, or c
[^abc]Not a, b, or c
[a-z]Character range a to z

Quantifiers

Key / CodeDescription
*0 or more
+1 or more
?0 or 1 (optional)
{3}Exactly 3 times
{2,5}Between 2 and 5 times

Anchors & Groups

Key / CodeDescription
^Start of string/line
$End of string/line
\bWord boundary
(...)Group
(?:...)Non-capturing group

Common Patterns

Key / CodeDescription
Email^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$
ISO Date (YYYY-MM-DD)^\d{4}-\d{2}-\d{2}$
Hex Color^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$
Knowledge is power.