This blog post gives an overview and examples of regular expression syntax as implemented by the re built-in module (Python 3.13+). Assume ASCII character set unless otherwise specified. This post is an excerpt from my Understanding Python re(gex)? book.
Visualization created using debuggex for the pattern r'\bpar(en|ro)?t\b'
From docs.python: re:
A regular expression (or RE) specifies a set of strings that matches it; the functions in this module let you check if a particular string matches a given regular expression
Elements that define a regular expression
| Anchors | Description |
|---|---|
\A | restricts the match to the start of string |
\Z | restricts the match to the end of string |
^ | restricts the match to the start of line |
$ | restricts the match to the end of line |
\n | newline character is used as the line separator |
re.MULTILINE or re.M | flag to treat input as multiline string |
\b | restricts the match to the start/end of words |
| word characters: alphabets, digits, underscore | |
\B | matches wherever \b doesn't match |
^, $ and \ are metacharacters in the above table, as these characters have special meaning. Prefix a \ character to remove the special meaning and match such characters literally. For example, \^ will match a ^ character instead of acting as an anchor.
| Feature | Description |
|---|---|
| | multiple RE combined as conditional OR |
| each alternative can have independent anchors | |
(pat) | group patterns, also a capturing group |
a(b|c)d is same as abd|acd | |
(?:pat) | non-capturing group |
(?P<name>pat) | named capture group |
. | Match any character except the newline character \n |
[] | Character class, matches one character among many |
| Greedy Quantifiers | Description |
|---|---|
* | Match zero or more times |
+ | Match one or more times |
? | Match zero or one times |
{m,n} | Match m to n times (inclusive) |
{m,} | Match at least m times |
{,n} | Match up to n times (including 0 times) |
{n} | Match exactly n times |
pat1.*pat2 | any number of characters between pat1 and pat2 |
pat1.*pat2|pat2.*pat1 | match both pat1 and pat2 in any order |
Greedy here means that the above quantifiers will match as much as possible that'll also honor the overall RE. Appending a ? to greedy quantifiers makes them non-greedy, i.e. match as minimally as possible. Appending a + to greedy quantifiers makes them possessive, which prevents backtracking. You can also use (?>pat) atomic grouping to safeguard from backtracking. Quantifiers can be applied to literal characters, groups, backreferences and character classes.
| Character class | Description |
|---|---|
[aeiou] | Match any vowel |
[^aeiou] | ^ inverts selection, so this matches any consonant |
[a-f] | - defines a range, so this matches any of abcdef characters |
\d | Match a digit, same as [0-9] |
\D | Match non-digits, same as [^0-9] or [^\d] |
\w | Match word characters, same as [a-zA-Z0-9_] |
\W | Match non-word characters, same as [^a-zA-Z0-9_] or [^\w] |
\s | Match whitespace characters, same as [\ \t\n\r\f\v] |
\S | Match non-whitespace characters, same as [^\ \t\n\r\f\v] or [^\s] |
| Lookarounds | Description |
|---|---|
| lookarounds | custom assertions, zero-width like anchors |
(?!pat) | negative lookahead assertion |
(?<!pat) | negative lookbehind assertion |
(?=pat) | positive lookahead assertion |
(?>> bool(re.search(r'^par$', 'spare\npar\ndare', flags=re.M))
True
Understanding Python re(gex)? bookVisit my GitHub repo Understanding Python re(gex)? for details about the book I wrote on Python regular expressions. The book uses plenty of examples to explain the concepts from the basics and introduces more advanced concepts step-by-step. The book also covers the third-party regex module. The cheatsheet and examples presented in this post are based on the contents of this book. You can get all my ebooks as a single bundle via leanpub or gumroad.
|