0% found this document useful (0 votes)
42 views

Python Regular Expression (Regex) Cheat Sheet: by Via

This document provides a cheat sheet for Python regular expressions (regex). It lists special characters and their meanings, common regex methods from the 're' module like 're.compile()', 're.search()', and 're.sub()', as well as attributes and methods of Match objects returned from regex matches. The cheat sheet is a concise reference for working with regex patterns and modules in Python.

Uploaded by

Dimitris Lyberis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Python Regular Expression (Regex) Cheat Sheet: by Via

This document provides a cheat sheet for Python regular expressions (regex). It lists special characters and their meanings, common regex methods from the 're' module like 're.compile()', 're.search()', and 're.sub()', as well as attributes and methods of Match objects returned from regex matches. The cheat sheet is a concise reference for working with regex patterns and modules in Python.

Uploaded by

Dimitris Lyberis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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, erl​apping occurr​ences 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 repeti​tions of RE string, match pattern return a flags=0)

+ Match 1 or more repeti​tions 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 repeti​tions of RE
pattern, the pattern return a match re.p​urg​e () 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"t​ext​" there is no
{m,n} Match from m to n repeti​tions 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.mat​ch(​r"\W​(.)​\1​\W", " ff
\ Escape special characters re.findall( Return all non-ov​erl​apping ")
[] Match a set of characters pattern, matches of pattern in string <r​e.Match object; span=(0, 4),
| RE1|​RE2: Match either RE1 or string, as list of strings. match=' ff '>
RE2 non-greedy flags=0) >>> re.mat​ch(​"​\\W​(.)​\\1​\\W​", "

(...) Match RE inside parant​heses and re.finditer( Return an iter​ator yielding ff ")
indicate start and end of a group pattern, match objects over all <r​e.Match object; span=(0, 4),
string, non-ov​erl​apping 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:​//d​ocs.py​tho​n.o​rg/​3/h​owt​o/r​ege​x.html
https:​//d​ocs.py​tho​n.o​rg/​3/l​ibr​ary​/re.html

Extensions

(?...) This is the start of an extension


(? The letters set the corres​pondig
aiLmsux) flags See flags
(?:...) A non-ca​pturing version of
regular parant​heses

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 l​ast​‐ matched capturing group, or
(?P=name) A backre​ference to a backslash substi​‐ index None.
named group tution on templ​ate, Match.​ The name of the last matched
as done by the l​ast​‐ capturing group or None
(?#...) A comment
sub() method group
(?=...) lookahead assert​ion:
Match.group( Returns one or Match.​ The regular expression object
Matches if ... matches next
[group1,...]) more subgroups of re whose matc​h() or sear​ch()
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 matc​h() or
arguments return a
match next s​tring sear​ch()
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 dict​ion​ary
assert​ion: Match if the
default=None) containing all the \d Match any Unic​ode 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-p​attern if
name. \s Match Unic​ode 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) pa​ttern 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 Unic​ode 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 sear​ch() or
matc​h() method of
the regex object
Match.​e​ndpos 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.​sea​rch​(). Patter​n.​gr​oups 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.​mat​ch() Patter​n.​pa​ttern The pattern string
pos[, from which the
endpos]]) pattern object was
Pattern.fullmatch( Likewise but see compiled
string[, re.​ful​lma​tch​() These objects are returned by the re.​‐
pos[,
com​pil​e() 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[, dal​l() 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[, dit​er() but with DOTALL, S dot matches all
pos[, additional parameters u unicode matching (just
endpos]]) pos and endpos in (?aiLm​sux))
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
Patter​n.​fl​ags 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

You might also like