python regular expression (regex) Cheat Sheet
by mutanclan (mutanclan) via cheatography.com/79625/cs/19404/
Special characters Methods of 're' module Methods of 're' module (cont)
. Default: Match any character re.compile( Compile a regular re.sub( Return the string obtained by
except newline pattern, expression pattern into a pattern, replacing the leftmost non-ov‐
. DOTALL: Match any character flags=0) regular expression object. repl, erlapping occurrences of
including newline Can be used with match(), string, pattern in string by the repla‐
search() and others count=0, cement repl. repl can be a
^ Default: Match the start of a string
re.search( Search through string flags=0) function.
^ MULTILINE: Match immediatly
pattern, matching the first location re.subn( Like sub but return a tuple
after each newline
string, of the RE. Returns a match pattern, (new_string,
$ Match the end of a string
flags=0 object or None repl, number_of_subs_made)
$ MULTILINE: Also match before a string,
re.match( If zero or more characters
newline count=0,
pattern, at the beginning of a string
* Match 0 or more repetitions of RE string, match pattern return a flags=0)
+ Match 1 or more repetitions of RE flags=0) match object or None re.escape( Escape special characters in
re.fullmatch( If the whole string matches pattern) pattern
? Match 0 or 1 repetitions of RE
pattern, the pattern return a match re.purge () Clear the regular expression
*?, *+, Match non-greedy as few
string, object or None cache
?? characters as possible
flags=0)
{m} Match exactly m copies of the
re.split( Split string by the occurr‐ Raw String Notation
previous RE
pattern, ences of pattern maxsplit In raw string notation r"text" there is no
{m,n} Match from m to n repetitions of
string, times if non-zero. Returns need to escape the backslash character
RE
maxsplit=0, a list of all groups. again.
{m,n}? Match non-greedy flags=0) >>> re.match(r"\W(.)\1\W", " ff
\ Escape special characters re.findall( Return all non-overlapping ")
[] Match a set of characters pattern, matches of pattern in string <re.Match object; span=(0, 4),
| RE1|RE2: Match either RE1 or string, as list of strings. match=' ff '>
RE2 non-greedy flags=0) >>> re.match("\\W(.)\\1\\W", "
(...) Match RE inside parantheses and re.finditer( Return an iterator yielding ff ")
indicate start and end of a group pattern, match objects over all <re.Match object; span=(0, 4),
string, non-overlapping matches match=' ff '>
With RE is the resulting regular expression.
flags=0) for the pattern in string
Reference
Special characters must be escaped with \ if
it should match the character literally https://docs.python.org/3/howto/regex.html
https://docs.python.org/3/library/re.html
Extensions
(?...) This is the start of an extension
(? The letters set the correspondig
aiLmsux) flags See flags
(?:...) A non-capturing version of
regular parantheses
By mutanclan (mutanclan) Published 19th April, 2019. Sponsored by Readable.com
cheatography.com/mutanclan/ Last updated 29th August, 2019. Measure your website readability!
Page 1 of 3. https://readable.com
python regular expression (regex) Cheat Sheet
by mutanclan (mutanclan) via cheatography.com/79625/cs/19404/
Extensions (cont) Match objects Match objects (cont)
(?P<na‐ Like regular paranthes but Match.expand( Return the string Match. The integer index of the last
me>...) with a named group template) obtained by doing last‐ matched capturing group, or
(?P=name) A backreference to a backslash substi‐ index None.
named group tution on template, Match. The name of the last matched
as done by the last‐ capturing group or None
(?#...) A comment
sub() method group
(?=...) lookahead assertion:
Match.group( Returns one or Match. The regular expression object
Matches if ... matches next
[group1,...]) more subgroups of re whose match() or search()
without consuming the
the match. 1 method produced this match
string
Argument returns instance
(?!...) negative lookahead assert‐
string and more
ion: Matches if ... doesn't Match. The string passed to match() or
arguments return a
match next string search()
tuple.
(?<=....) positive lookbehind assert‐
Match.__getitem__( Access groups with Special escape characters
ion: Match if the current
g) m[0], m[1] ...
position in the string is \A Match only at the start of the string
Match.groups( Return a tuple
preceded by a match for ... \b Match the empty string at the
default=None) containing all the
that ends the current beginning or end of a word
subgroups of the
position
match \B Match the empty string when not at
(?<!...) negative lookbehind the beginning or end of a word
Match.groupdict( Return a dictionary
assertion: Match if the
default=None) containing all the \d Match any Unicode decimal digit this
current position in the
named subgroups includes [0-9]
string is not preceded by a
of the match, keyed \D Match any character which is not a
match for ...
by the subgroup decimal digit
(? Match with yes-pattern if
name. \s Match Unicode white space
(id/name)yes- the group with gived id or
Match.start( Return the indices characters which includes [ \t\n\r\f\v]
pattern|no- name exists and with no-
[group] of the start and end \S Matches any character which is not a
pattern) pattern if not
Match.end( of the substring whitespace character. The opposite of
[group]) matched by group \s
Match.span( For a match m, \w Match Unicode word characters
[group]) return the 2-tuple including [a-zA-Z0-9_]
(m.start(group)
\W Match the opposite of \w
m.end(group))
\Z Match only at the end of a string
Match.pos The value of pos
which was passed
to the search() or
match() method of
the regex object
Match.endpos Likewise but the
value of endpos
By mutanclan (mutanclan) Published 19th April, 2019. Sponsored by Readable.com
cheatography.com/mutanclan/ Last updated 29th August, 2019. Measure your website readability!
Page 2 of 3. https://readable.com
python regular expression (regex) Cheat Sheet
by mutanclan (mutanclan) via cheatography.com/79625/cs/19404/
Regular Expression Objects Regular Expression Objects (cont)
Pattern.search( See re.search(). Pattern.groups The number of
string[, pos gives an index capturing groups in
pos[, where to start the the pattern
endpos]]) search. endpos limits Pattern.groupindex A dictionary mapping
how far the string will any symbolic group
be searched. names to group
Pattern.match( Likewise but see members
string[, re.match() Pattern.pattern The pattern string
pos[, from which the
endpos]]) pattern object was
Pattern.fullmatch( Likewise but see compiled
string[, re.fullmatch() These objects are returned by the re.‐
pos[,
compile() method
endpos]])
Pattern.split( Identical to re.spl‐ Flags
string, it()
ASCII, A ASCII-only matching in
maxsplit=0)
\w, \b, \s and \d
Pattern.findall( Similar to re.fin‐
IGNORECASE, I ignore case
string[, dall() but with
pos[, LOCALE, L do a local-aware match
additional parameters
endpos]]) pos and endpos MULTILINE, M multiline matching,
affecting ^ and $
Pattern.finditer( Similar to re.fin‐
string[, diter() but with DOTALL, S dot matches all
pos[, additional parameters u unicode matching (just
endpos]]) pos and endpos in (?aiLmsux))
Pattern.sub( Identical to re.sub() VERBOSE, X verbose
repl,
Flags are used in (?aiLmsux-imsx:...) or (?
string,
aiLmsux) or can be accessed with
count=0)
re.FLAG. In the first form flags are set or
Pattern.subn( Identical to re.sub‐ removed.
repl, n()
string, This is useful if you wish to include the flags
count=0) as part of the regular expression, instead of
Pattern.flags The regex matching passing a flag argument to the re.compile()
flags. function
By mutanclan (mutanclan) Published 19th April, 2019. Sponsored by Readable.com
cheatography.com/mutanclan/ Last updated 29th August, 2019. Measure your website readability!
Page 3 of 3. https://readable.com