Cheatsheet
Metacharacters
.
Any single character except newline\d
Any digit character (0-9)\D
Any non-digit character\s
Any whitespace character (space, tab, newline, etc.)\S
Any non-whitespace character\w
Any word character (letter, digit, underscore)\W
Any non-word character\b
Word boundary\B
Non-word boundary
Quantifiers
*
Matches the preceding item zero or more times+
Matches the preceding item one or more times?
Matches the preceding item zero or one time{n}
Matches the preceding item exactly n times{n,}
Matches the preceding item n or more times{n,m}
Matches the preceding item at least n times, but no more than m times
Anchors
^
Matches the start of the string$
Matches the end of the string\b
Matches a word boundary\B
Matches a non-word boundary
Groups
()
Matches the group inside the parentheses(?:)
Matches the group inside the parentheses, but doesn't capture the match(?=)
Positive lookahead. Matches the group inside the parentheses, but doesn't include it in the match(?!)
Negative lookahead. Matches if the group inside the parentheses doesn't match(?<name>)
Matches the group inside the parentheses and captures it using the specified name(?P<name>)
Matches the group inside the parentheses and captures it using the specified name (Python syntax)
Repetition
*
Match 0 or more of the preceding character+
Match 1 or more of the preceding character?
Match 0 or 1 of the preceding character{n}
Match exactly n of the preceding character{n,}
Match at least n of the preceding character{n,m}
Match at least n but not more than m of the preceding character
Escape Sequences
\d
Match any digit character (0-9)\D
Match any non-digit character\w
Match any word character (a-z, A-Z, 0-9, _)\W
Match any non-word character\s
Match any whitespace character (space, tab, newline, etc.)\S
Match any non-whitespace character\b
Match a word boundary\B
Match a non-word boundary\n
Match a newline character\r
Match a carriage return character\t
Match a tab character\f
Match a form feed character\v
Match a vertical tab character
Alternation
|
Match either the expression before or after the | character( )
Group expressions together to match as a unit(?: )
Group expressions together, but don't capture the group(?= )
Positive lookahead - Match if the expression inside the parentheses matches the input string, but don't include it in the match(?! )
Negative lookahead - Match if the expression inside the parentheses does not match the input string