Python Re Cheat Sheet



Introduction to Python 3 Cheat Sheet. Python is a high-level programming language that was designed by Guido Van Rossum and developed by the Python software foundation in 1990. It has a paradigm of Object-oriented, imperative and procedural reflective. The parent company of python is a Python. Python 3 Beginner's Reference Cheat Sheet Alvaro Sebastian Legend: x,y stand for any kind of data values, s for a string, n for a number, L for a list where i,j are list indexes, D stands for a dictionary and k is a dictionary key. Reading and writing files f = open(,'w') f.write f.close. Python Cheat Sheet Christina Costello February 20, 2020 09:24. Re.match - only find matches if they occur at the start of the string being searched.

Above visualization is a screenshot created usingdebuggexfor the patternr'bpar(en|ro)?tb'


Python regex cheat sheet examples

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

This blog post gives an overview and examples of regular expression syntax as implemented by the re built-in module (Python 3.8+). Assume ASCII character set unless otherwise specified. This post is an excerpt from my Python re(gex)? book.

Elements that define a regular expressionđź”—

AnchorsDescription
Arestricts the match to the start of string
Zrestricts the match to the end of string
^restricts the match to the start of line
$restricts the match to the end of line
nnewline character is used as line separator
re.MULTILINE or re.Mflag to treat input as multiline string
brestricts the match to the start/end of words
word characters: alphabets, digits, underscore
Bmatches 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.

FeatureDescription
|multiple RE combined as conditional OR
each alternative can have independent anchors
(RE)group pattern(s), also a capturing group
a(b|c)d is same as abd|acd
(?:RE)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 QuantifiersDescription
*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.*pat2any number of characters between pat1 and pat2
pat1.*pat2|pat2.*pat1match 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. Quantifiers can be applied to literal characters, groups, backreferences and character classes.

Character classDescription
[aeiou]Match any vowel
[^aeiou]^ inverts selection, so this matches any consonant
[a-f]- defines a range, so this matches any of abcdef characters
dMatch a digit, same as [0-9]
DMatch non-digit, same as [^0-9] or [^d]
wMatch word character, same as [a-zA-Z0-9_]
WMatch non-word character, same as [^a-zA-Z0-9_] or [^w]
sMatch whitespace character, same as [ tnrfv]
SMatch non-whitespace character, same as [^ tnrfv] or [^s]
LookaroundsDescription
lookaroundscustom assertions, zero-width like anchors
(?!pat)negative lookahead assertion
(?<!pat)negative lookbehind assertion
(?=pat)positive lookahead assertion
(?<=pat)positive lookbehind assertion
(?!pat1)(?=pat2)multiple assertions can be specified in any order
as they mark a matching location without consuming characters
((?!pat).)*Negate a grouping, similar to negated character class
FlagsDescription
re.IGNORECASE or re.Iflag to ignore case
re.DOTALL or re.Sallow . metacharacter to match newline character
flags=re.S|re.Imultiple flags can be combined using | operator
re.MULTILINE or re.Mallow ^ and $ anchors to match line wise
re.VERBOSE or re.Xallows to use literal whitespaces for aligning purposes
and to add comments after the # character
escape spaces and # if needed as part of actual RE
re.ASCII or re.Amatch only ASCII characters for b, w, d, s
and their opposites, applicable only for Unicode patterns
re.LOCALE or re.Luse locale settings for byte patterns and 8-bit locales
(?#comment)another way to add comments, not a flag
(?flags:pat)inline flags only for this pat, overrides flags argument
flags is i for re.I, s for re.S, etc, except L for re.L
(?-flags:pat)negate flags only for this pat
(?flags-flags:pat)apply and negate particular flags only for this pat
(?flags)apply flags for whole RE, can be used only at start of RE
anchors if any, should be specified after (?flags)
Matched portionDescription
re.Match objectdetails like matched portions, location, etc
m[0] or m.group(0)entire matched portion of re.Match object m
m[n] or m.group(n)matched portion of nth capture group
m.groups()tuple of all the capture groups' matched portions
m.span()start and end+1 index of entire matched portion
pass a number to get span of that particular capture group
can also use m.start() and m.end()
Nbackreference, gives matched portion of Nth capture group
applies to both search and replacement sections
possible values: 1, 2 up to 99 provided no more digits
g<N>backreference, gives matched portion of Nth capture group
possible values: g<0>, g<1>, etc (not limited to 99)
g<0> refers to entire matched portion
(?P<name>pat)named capture group
refer as 'name' in re.Match object
refer as (?P=name) in search section
refer as g<name> in replacement section
groupdictmethod applied on a re.Match object
gives named capture group portions as a dict

0 and 100 onwards are considered as octal values, hence cannot be used as backreferences.

re module functionsđź”—

FunctionDescription
re.searchCheck if given pattern is present anywhere in input string
Output is a re.Match object, usable in conditional expressions
r-strings preferred to define RE
Use byte pattern for byte input
Python also maintains a small cache of recent RE
re.fullmatchensures pattern matches the entire input string
re.compileCompile a pattern for reuse, outputs re.Pattern object
re.subsearch and replace
re.sub(r'pat', f, s)function f with re.Match object as argument
re.escapeautomatically escape all metacharacters
re.splitsplit a string based on RE
text matched by the groups will be part of the output
portion matched by pattern outside group won't be in output
re.findallreturns all the matches as a list
if 1 capture group is used, only its matches are returned
1+, each element will be tuple of capture groups
portion matched by pattern outside group won't be in output
re.finditeriterator with re.Match object for each match
re.subngives tuple of modified string and number of substitutions

The function definitions are given below:


Regular expression examplesđź”—

Regular Expressions In Python

As a good practice, always use raw strings to construct RE, unless other formats are required. This will avoid clash of special meaning of backslash character between RE and normal quoted strings.

  • examples for re.search
  • difference between string and line anchors
  • examples for re.findall
  • examples for re.split
  • backreferencing within search pattern
  • working with matched portions
  • examples for re.finditer
  • examples for re.sub
  • backreferencing in replacement section
  • using functions in replacement section of re.sub
  • examples for lookarounds
  • examples for re.compile

Regular expressions can be compiled using re.compile function, which gives back a re.Pattern object. The top level re module functions are all available as methods for this object. Compiling a regular expression helps if the RE has to be used in multiple places or called upon multiple times inside a loop (speed benefit). By default, Python maintains a small list of recently used RE, so the speed benefit doesn't apply for trivial use cases.


Python re(gex)? bookđź”—

Visit my repo Python re(gex)? for details about the book I wrote on Python regular expressions. The ebook uses plenty of examples to explain the concepts from the very beginning and step by step introduces more advanced concepts. The book also covers the third party module regex. The cheatsheet and examples presented in this post are based on contents of this book.

Use this leanpub link for a discounted price.

By Alex Yang, Dataquest

This cheat sheet is based on Python 3’s documentation on regular expressions. If you're interested in learning Python, we have a free Python Programming: Beginner course for you to try out.


Download the cheat sheet here

Special Characters


^ | Matches the expression to its right at the start of a string. It matches every such instance before each n in the string.

$ | Matches the expression to its left at the end of a string. It matches every such instance before each n in the string.

. | Matches any character except line terminators like n.

| Escapes special characters or denotes character classes.

A|B | Matches expression A or B. If A is matched first, B is left untried.

+ | Greedily matches the expression to its left 1 or more times.

* | Greedily matches the expression to its left 0 or more times.

? | Greedily matches the expression to its left 0 or 1 times. But if ? is added to qualifiers (+, *, and ? itself) it will perform matches in a non-greedy manner.

{m} | Matches the expression to its left m times, and not less.

{m,n} | Matches the expression to its left m to n times, and not less.

{m,n}? | Matches the expression to its left m times, and ignores n. See ? above.

Character Classes (a.k.a. Special Sequences)


w | Matches alphanumeric characters, which means a-z, A-Z, and 0-9. It also matches the underscore, _.

d | Matches digits, which means 0-9.

D | Matches any non-digits.

s | Matches whitespace characters, which include the t, n, r, and space characters.

S | Matches non-whitespace characters.

b | Matches the boundary (or empty string) at the start and end of a word, that is, between w and W.

B | Matches where b does not, that is, the boundary of w characters.

A | Matches the expression to its right at the absolute start of a string whether in single or multi-line mode.

Z | Matches the expression to its left at the absolute end of a string whether in single or multi-line mode.

Python Re Expression Cheat Sheet

Sets


[ ] | Contains a set of characters to match.

[amk] | Matches either a, m, or k. It does not match amk.

[a-z] | Matches any alphabet from a to z.

[a-z] | Matches a, -, or z. It matches - because escapes it.

[a-] | Matches a or -, because - is not being used to indicate a series of characters.

[-a] | As above, matches a or -.

[a-z0-9] | Matches characters from a to z and also from 0 to 9.

[(+*)] | Special characters become literal inside a set, so this matches (, +, *, and ).

[^ab5] | Adding ^ excludes any character in the set. Here, it matches characters that are not a, b, or 5.

Groups


( ) | Matches the expression inside the parentheses and groups it.

(? ) | Inside parentheses like this, ? acts as an extension notation. Its meaning depends on the character immediately to its right.

(?PAB) | Matches the expression AB, and it can be accessed with the group name.

Python Regex Cheat Sheet Pdf

(?aiLmsux) | Here, a, i, L, m, s, u, and x are flags:

  • a — Matches ASCII only
  • i — Ignore case
  • L — Locale dependent
  • m — Multi-line
  • s — Matches all
  • u — Matches unicode
  • x — Verbose

(?:A) | Matches the expression as represented by A, but unlike (?PAB), it cannot be retrieved afterwards.

(?#...) | A comment. Contents are for us to read, not for matching.

A(?=B) | Lookahead assertion. This matches the expression A only if it is followed by B.

A(?!B) | Negative lookahead assertion. This matches the expression A only if it is not followed by B.

(?<=B)A | Positive lookbehind assertion. This matches the expression A only if B is immediately to its left. This can only matched fixed length expressions.

(?<!B)A | Negative lookbehind assertion. This matches the expression A only if B is not immediately to its left. This can only matched fixed length expressions.

(?P=name) | Matches the expression matched by an earlier group named “name”.

(...)1 | The number 1 corresponds to the first group to be matched. If we want to match more instances of the same expresion, simply use its number instead of writing out the whole expression again. We can use from 1 up to 99 such groups and their corresponding numbers.

Popular Python re module Functions


re.findall(A, B) | Matches all instances of an expression A in a string B and returns them in a list.

Python Regex List

re.search(A, B) | Matches the first instance of an expression A in a string B, and returns it as a re match object.

re.split(A, B) | Split a string B into a list using the delimiter A.

re.sub(A, B, C) | Replace A with B in the string C.


Useful Regular Expressions Sites for Python users


Bio: Alex Yang is a writer fascinated by the things code can do. He also enjoys citizen science and new media art.

Python Regex Cheat Sheet Datacamp

List

Original. Reposted with permission.

Python Regex Symbols

Related:





Comments are closed.