Regular Expression Cheat Sheet for Dummies

Regex BasicsDescription
^The start of a string
$The end of a string
.Wildcard which matches any character, except newline (n).
|Matches a specific character or group of characters on either side (e.g. a|b corresponds to a or b)
Used to escape a special character
aThe character “a”
abThe string “ab”
*Used to match 0 or more of the previous (e.g. xy*z could correspond to “xz”, “xyz”, “xyyz”, etc.
?Matches 0 or 1 of the previous
+Matches 1 or more of the previous
{5}Matches exactly 5
{5, 10}Matches everything between 5-10
Character ClassesDescription
sMatches a whitespace character
SMatches a non-whitespace character
wMatches a word character
WMatches a non-word character
dMatches one digit
DMatches one non-digit
[b]A backspace character
cA control character
Special CharactersDescription
nMatches a newline
tMatches a tab
rMatches a carriage return
ZZZMatches octal character ZZZ
xZZMatches hex character ZZ
A null character
vA vertical tab
(xyz)Grouping of characters
(?:xyz)Non-capturing group of characters
[xyz]Matches a range of characters (e.g. x or y or z)
[^xyz]Matches a character other than x or y or z
[a-q]Matches a character from within a specified range
[0-7]Matches a digit from within a specified range
String ReplacementsDescription
$`Insert before matched string
$’Insert after matched string
$+Insert last matched
$&Insert entire match
$nInsert nth captured group
(?=xyz)Positive lookahead
(?!xyz)Negative lookahead
?!= or ?<!Negative lookbehind
bWord Boundary (usually a position between /w and /W)


One Reply to “Regular Expression Cheat Sheet for Dummies”

Leave a Reply

Your email address will not be published. Required fields are marked *