Python Regex Cheat Sheet

This page provides a Python regex cheat sheet that you can quickly reference while working with regular expressions.

Character sets

PatternMeaning
\wMatch a single word character a-z, A-Z, 0-9, and underscore (_)
\dMatch a single digit 0-9
\sMatch whitespace including \t, \n, and \r and space character
.Match any character except the newline
\WMatch a character except for a word character
\DMatch a character except for a digit
\SMatch a single character except for a whitespace character

Anchors

PatternMeaning
^Match at the beginning of a string
$Match at the end of a string
\bMatch a position defined as a word boundary
\BMatch a position that is not a word boundary

Quantifiers

Quantifiers (Greedy)Non-greedy Quantifiers (Lazy)Meaning
**?Match its preceding element zero or more times.
++?Match its preceding element one or more times.
???Match its preceding element zero or one time.
{n}{n}?Match its preceding element exactly n times.
{n , }{n,}?Match its preceding element at least n times.
{n , m}{n , m}?Match its preceding element from n to m times

Sets & Ranges

PatternMeaning
[XYZ]Match any of three elements X, Y, and Z
[X-Y]Match a range from X to Y
^[XYZ]Match any single element except X, Y, and Z
^[X-Y]Match any single element
{n , }Match its preceding element at least n times.
{n , m}Match its preceding element from n to m times

Capturing Groups

PatternMeaning
(X)Capture the X in the group
(?P<name>X)Capture the X and assign it the name
\NReference the capturing group #N
\g<N>Reference the capturing group #N (alternative syntax)

Alternation

PatternMeaning
X | YMatch either X or Y

Look Around

PatternMeaning
X(?=Y)Match X but only if it is followed by Y
X(?!Y)Match X but only if it is NOT followed by Y
(?<=Y)XMatch X if there is Y before it
(?<!Y)XMatch X if there is NO Y before it

Regex functions

The following table shows the regex function from the re module.

FunctionDescription
findall()Return a list of matches or None
finditer()Return an iterator yielding all non-overlapping matches
search()Return the first match
fullmatch()Return a Match object if the whole string matches a pattern
match()Return the match at the beginning of a string or None
sub()Return a string with matched replaced with a replacement
split()Split a string at the occurrences of matches

Regex Flags

FlagAliasInline FlagMeaning
re.ASCIIre.A?mThe re.ASCII is relevant to the byte patterns only. It makes the \w, \W,\b, \B, \d, \D, and \S perform ASCII-only matching instead of full Unicode matching.
re.DEBUGN/AN/AThe re.DEBUG shows the debug information of compiled pattern.
re.IGNORECASEre.I?iperform case-insensitive matching. It means that the [A-Z] will also match lowercase letters.
re.LOCALEre.L?LThe re.LOCALE is relevant only to the byte pattern. It makes the \w, \W, \b, \B and case-sensitive matching dependent on the current locale. The re.LOCALE is not compatible with the re.ASCII flag.
re.MUTILINEre.M?mThe re.MULTILINE makes the ^ matches at the beginning of a string and at the beginning of each line and $ matches at the end of a string and at the end of each line.
re.DOTALLre.S?sBy default, the dot (.) matches any characters except a newline. The re.DOTALL makes the dot (.) matches all characters including a newline.
re.VERBOSEre.X?xThe re.VERBOSE flag allows you to organize a pattern into logical sections visually and add comments.

Comments

Leave a Reply

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