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
Syntax
Description
.
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
Character
Description
\
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
\s
Matches any whitespace character
\S
Matches any non-whitespace character
\d
Matches any digit
\D
Matches any non-digit
\w
Matches any word character
\W
Matches any non-word character
\b
Matches a word boundary
\B
Matches a non-word boundary
\n
Matches a newline character
\t
Matches a tab character
\r
Matches a carriage return character
Character Classes
Character
Description
[]
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
Character
Description
?
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
Character
Description
^
Matches the start of the string or line
$
Matches the end of the string or line
\A
Matches the start of the string
\Z
Matches the end of the string
\b
Matches a word boundary
\B
Matches a non-word boundary
Alternation
Character
Description
`
`
Grouping
Character
Description
()
Groups regular expressions together
(?P<name>)
Named capture group
(?:)
Non-capturing group
Backreferences
Character
Description
\number
Matches the same text as previously matched by the capturing group numbered number