Python Regular Expressions Reference

Complete guide to Python's re module patterns and constructs

Based on Python's re module documentation. This reference covers all regular expression constructs available in Python's built-in re module, including special characters, character classes, quantifiers, groups, lookahead/lookbehind assertions, and more.

Showing 48 of 48 constructs

Special Characters8 constructs
ConstructMatchesDescriptionAction
.Any character except newlineMatches any single character except newline (\n)
^Start of stringMatches the start of the string
$End of stringMatches the end of the string
*0 or more repetitionsMatches 0 or more repetitions of the preceding RE
+1 or more repetitionsMatches 1 or more repetitions of the preceding RE
?0 or 1 repetitionMatches 0 or 1 repetitions of the preceding RE
\Escape characterEscapes special characters or signals a special sequence
|AlternationA|B matches either A or B
Character Classes4 constructs
ConstructMatchesDescriptionAction
[abc]Any of a, b, or cMatches any single character in the brackets
[^abc]Any character except a, b, or cMatches any single character not in the brackets
[a-z]Any lowercase letterMatches any character in the range from a to z
[0-9]Any digitMatches any digit from 0 to 9
Predefined Character Classes6 constructs
ConstructMatchesDescriptionAction
\dAny decimal digitEquivalent to [0-9]
\DAny non-digit characterEquivalent to [^0-9]
\sAny whitespace characterMatches [ \t\n\r\f\v]
\SAny non-whitespace characterEquivalent to [^ \t\n\r\f\v]
\wAny word characterEquivalent to [a-zA-Z0-9_]
\WAny non-word characterEquivalent to [^a-zA-Z0-9_]
Quantifiers6 constructs
ConstructMatchesDescriptionAction
{n}Exactly n repetitionsMatches exactly n copies of the preceding RE
{n,}n or more repetitionsMatches n or more copies of the preceding RE
{n,m}Between n and m repetitionsMatches from n to m copies of the preceding RE
*?0 or more (non-greedy)Non-greedy version of *
+?1 or more (non-greedy)Non-greedy version of +
??0 or 1 (non-greedy)Non-greedy version of ?
Anchors4 constructs
ConstructMatchesDescriptionAction
\bWord boundaryMatches empty string at word boundaries
\BNon-word boundaryMatches empty string when not at word boundary
\AStart of stringMatches only at the start of the string
\ZEnd of stringMatches only at the end of the string
Groups and Capturing5 constructs
ConstructMatchesDescriptionAction
(pattern)Capturing groupMatches pattern and captures the match
(?:pattern)Non-capturing groupMatches pattern but does not capture the match
(?P<name>pattern)Named capturing groupMatches pattern and captures with given name
\1Backreference to group 1Matches the same text as previously matched by group 1
(?P=name)Named backreferenceMatches the same text as previously matched by named group
Lookahead and Lookbehind4 constructs
ConstructMatchesDescriptionAction
(?=pattern)Positive lookaheadMatches if pattern matches next, but does not consume
(?!pattern)Negative lookaheadMatches if pattern does not match next
(?<=pattern)Positive lookbehindMatches if pattern matches before current position
(?<!pattern)Negative lookbehindMatches if pattern does not match before current position
Flags and Modifiers4 constructs
ConstructMatchesDescriptionAction
(?i)Case insensitiveMakes the pattern case insensitive (re.IGNORECASE)
(?m)Multiline modeMakes ^ and $ match line boundaries (re.MULTILINE)
(?s)Dot matches allMakes . match any character including newline (re.DOTALL)
(?x)Verbose modeAllows whitespace and comments in pattern (re.VERBOSE)
Escape Sequences7 constructs
ConstructMatchesDescriptionAction
\tTab characterMatches a tab character
\nNewline characterMatches a newline character
\rCarriage returnMatches a carriage return character
\fForm feedMatches a form feed character
\vVertical tabMatches a vertical tab character
\oooOctal characterCharacter with octal value ooo
\xhhHex characterCharacter with hex value hh