Python Regular Expression (Regex) Cheat Sheet: by Via
Python Regular Expression (Regex) Cheat Sheet: by Via
. 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
(?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