Python Regex Cheat Sheet

Python regular expressions, or regex, are a powerful tool that allow you to search and manipulate text using a set of rules. They’re widely used in various applications like text processing, data validation, and web scraping.

Although regex syntax can be daunting at first, it becomes easier with practice and understanding of the basic rules. To help you get started, this cheat sheet covers everything you need to know about Python regex.

The cheat sheet is divided into sections that cover specific topics related to regex. The first section explains how to match characters and character classes using basic syntax. The second section covers special characters, such as anchors and quantifiers, which enable you to match specific parts of a string. The third section describes grouping and backreferences, which help you manipulate the matched text. The fourth section explains lookarounds, which let you match text based on what comes before or after it. Finally, the fifth section details flags, which modify the behavior of the regex engine.

Each section presents the information in a simple table format. On the left, there’s a list of characters or syntax rules, and on the right, there’s a brief explanation of how to use them.

This cheat sheet is a valuable resource for anyone looking to master Python regex, regardless of their experience level. It will enable you to write complex regex patterns quickly and easily, so you can match any text you need. Great, yeah? See the cheat sheet below!

Cheat Sheet

Basic Syntax

SyntaxDescription
.Matches any character except a newline
^Matches the start of the string
$Matches the end of the string
[]Matches any one of the enclosed characters
()Groups regular expressions together
``
?Matches zero or one of the preceding expression
*Matches zero or more of the preceding expression
+Matches one or more of the preceding expression
{m,n}Matches the preceding expression between m and n times
{m,}Matches the preceding expression m or more times
{,n}Matches the preceding expression up to n times

Special Characters

CharacterDescription
\Escapes a special character
^Matches the start of the string or line
$Matches the end of the string or line
.Matches any character except a newline
\sMatches any whitespace character
\SMatches any non-whitespace character
\dMatches any digit
\DMatches any non-digit
\wMatches any word character
\WMatches any non-word character
\bMatches a word boundary
\BMatches a non-word boundary
\nMatches a newline character
\tMatches a tab character
\rMatches a carriage return character

Character Classes

CharacterDescription
[]Matches any one of the enclosed characters
[a-z]Matches any lowercase letter from a to z
[A-Z]Matches any uppercase letter from A to Z
[0-9]Matches any digit from 0 to 9
[a-zA-Z0-9]Matches any alphanumeric character
[^]Matches any character not in the enclosed characters
[\d]Matches any digit
[\D]Matches any non-digit
[\s]Matches any whitespace character
[\S]Matches any non-whitespace character
[\w]Matches any word character
[\W]Matches any non-word character

Quantifiers

CharacterDescription
?Matches zero or one of the preceding expression
*Matches zero or more of the preceding expression
+Matches one or more of the preceding expression
{m,n}Matches the preceding expression between m and n times
{m,}Matches the preceding expression m or more times
{,n}Matches the preceding expression up to n times
*?Matches zero or more times, but as few as possible
+?Matches one or more times, but as few as possible
??Matches zero or one time, but as few as possible
{m,n}?Matches the preceding expression between m and n times, but as few as possible
{m,}?Matches the preceding expression m or more times, but as few as possible
{,n}?Matches the preceding expression up to n times, but as few as possible

Anchors

CharacterDescription
^Matches the start of the string or line
$Matches the end of the string or line
\AMatches the start of the string
\ZMatches the end of the string
\bMatches a word boundary
\BMatches a non-word boundary

Alternation

CharacterDescription
``

Grouping

CharacterDescription
()Groups regular expressions together
(?P<name>)Named capture group
(?:)Non-capturing group

Backreferences

CharacterDescription
\numberMatches the same text as previously matched by the capturing group numbered number

Lookarounds

CharacterDescription
(?=…)Positive lookahead
(?!…)Negative lookahead
(?<=…)Positive lookbehind
(?<!…)Negative lookbehind

Flags

FlagDescription
re.ICase-insensitive matching
re.MMultiline matching
re.SDot matches newline characters
re.UUnicode matching
re.XVerbose regular expressions

Reference:

https://docs.python.org/3/library/re.html