Python regular expression (regex) Cheat Sheet by mutanclan - Download free from Cheatography - Cheatography.com: Cheat Sheets For Every Occasion Download This Cheat Sheet (PDF). The Python RegEx Cheat Sheet for Budding Programmers Get Creative When Using Python Learning Python's regular expressions is a big step towards becoming a better Python programmer, but that's just one of the few things you need to do. However, playing around with its syntaxes and getting creative with them polishes your coding skill. Python unittest Assertions Enjoy this cheat sheet at its fullest within Dash, the macOS documentation browser. Regular Expressions Cheat Sheet by DaveChild - Cheatography.com Created Date: 4237Z.
Python Home
Introduction
Running Python Programs (os, sys, import)
Modules and IDLE (Import, Reload, exec)
Object Types - Numbers, Strings, and None
Strings - Escape Sequence, Raw String, and Slicing
Strings - Methods
Formatting Strings - expressions and method calls
Files and os.path
Traversing directories recursively
Subprocess Module
Regular Expressions with Python
Regular Expressions Cheat Sheet
Object Types - Lists
Object Types - Dictionaries and Tuples
Functions def, *args, **kargs
Functions lambda
Built-in Functions
map, filter, and reduce
Decorators
List Comprehension
Sets (union/intersection) and itertools - Jaccard coefficient and shingling to check plagiarism
Hashing (Hash tables and hashlib)
Dictionary Comprehension with zip
The yield keyword
Generator Functions and Expressions
generator.send() method
Iterators
Classes and Instances (__init__, __call__, etc.)
if__name__ '__main__'
argparse
Exceptions
@static method vs class method
Private attributes and private methods
bits, bytes, bitstring, and constBitStream
json.dump(s) and json.load(s)
Python Object Serialization - pickle and json
Python Object Serialization - yaml and json
Priority queue and heap queue data structure
Graph data structure
Dijkstra's shortest path algorithm
Prim's spanning tree algorithm
Closure
Functional programming in Python
Remote running a local file using ssh
SQLite 3 - A. Connecting to DB, create/drop table, and insert data into a table
SQLite 3 - B. Selecting, updating and deleting data
MongoDB with PyMongo I - Installing MongoDB ...
Python HTTP Web Services - urllib, httplib2
Web scraping with Selenium for checking domain availability
REST API : Http Requests for Humans with Flask
Blog app with Tornado
Multithreading ...
Python Network Programming I - Basic Server / Client : A Basics
Python Network Programming I - Basic Server / Client : B File Transfer
Python Network Programming II - Chat Server / Client
Python Network Programming III - Echo Server using socketserver network framework
Python Network Programming IV - Asynchronous Request Handling : ThreadingMixIn and ForkingMixIn
Python Coding Questions I
Python Coding Questions II
Python Coding Questions III
Python Coding Questions IV
Python Coding Questions V
Python Coding Questions VI
Python Coding Questions VII
Python Coding Questions VIII
Image processing with Python image library Pillow
Python and C++ with SIP
PyDev with Eclipse
Matplotlib
Redis with Python
NumPy array basics A
NumPy Matrix and Linear Algebra
Pandas with NumPy and Matplotlib
Celluar Automata
Batch gradient descent algorithm
Longest Common Substring Algorithm
Python Unit Test - TDD using unittest.TestCase class
Simple tool - Google page ranking by keywords
Google App Hello World
Google App webapp2 and WSGI
Uploading Google App Hello World
Python 2 vs Python 3
virtualenv and virtualenvwrapper
Uploading a big file to AWS S3 using boto module
Scheduled stopping and starting an AWS instance
Cloudera CDH5 - Scheduled stopping and starting services
Removing Cloud Files - Rackspace API with curl and subprocess
Checking if a process is running/hanging and stop/run a scheduled task on Windows
Apache Spark 1.3 with PySpark (Spark Python API) Shell
Apache Spark 1.2 Streaming
bottle 0.12.7 - Fast and simple WSGI-micro framework for small web-applications ...
Flask app with Apache WSGI on Ubuntu14/CentOS7 ...
Selenium WebDriver
Fabric - streamlining the use of SSH for application deployment
Ansible Quick Preview - Setting up web servers with Nginx, configure enviroments, and deploy an App
Neural Networks with backpropagation for XOR using one hidden layer
NLP - NLTK (Natural Language Toolkit) ...
RabbitMQ(Message broker server) and Celery(Task queue) ...
OpenCV3 and Matplotlib ...
Simple tool - Concatenating slides using FFmpeg ...
iPython - Signal Processing with NumPy
iPython and Jupyter - Install Jupyter, iPython Notebook, drawing with Matplotlib, and publishing it to Github
iPython and Jupyter Notebook with Embedded D3.js
Downloading YouTube videos using youtube-dl embedded with Python
Machine Learning : scikit-learn ...
Django 1.6/1.8 Web Framework ...
Special characters
.Default: Match any character except newline.DOTALL: Match any character including newline^Default: Match the start of a string^MULTILINE: Match immediatly after each newline$Match the end of a string$MULTILINE: Also match before a newline*Match 0 or more repetitions of RE+Match 1 or more repetitions of RE?Match 0 or 1 repetitions of RE*?, *+, ??Match non-greedy as few characters as possible{m}Match exactly m copies of the previous RE{m,n}Match from m to n repetitions of RE{m,n}?Match non-greedyEscape special characters[]Match a set of characters|RE1|RE2: Match either RE1 or RE2 non-greedy(...)Match RE inside parantheses and indicate start and end of a groupWith RE is the resulting regular expression.Special characters must be escaped with if it should match the character literally
Methods of 're' module
re.compile(pattern,
flags=0)Compile a regular expression pattern into a regular expression object. Can be used with match(), search() and othersre.search(
pattern,
string,
flags=0Search through string matching the first location of the RE. Returns a match object or Nonere.match(
pattern,
string,
flags=0)If zero or more characters at the beginning of a string match pattern return a match object or Nonere.fullmatch(
pattern,
string,
flags=0)If the whole string matches the pattern return a match object or Nonere.split(
pattern,
string,
maxsplit=0,
flags=0)Split string by the occurrences of patternmaxsplit times if non-zero. Returns a list of all groups.re.findall(
pattern,
string,
flags=0)Return all non-overlapping matches of pattern in string as list of strings.re.finditer(
pattern,
string,
flags=0)Return an iterator yielding match objects over all non-overlapping matches for the pattern in stringre.sub(
pattern,
repl,
string,
count=0,
flags=0)Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement
Regex In Python Cheat Sheet Examples
repl. repl can be a function.re.subn(
pattern,
repl,
string,
count=0,
flags=0)Like sub but return a tuple (new_string, number_of_subs_made)re.escape(
pattern)Escape special characters in pattern
pattern,
repl,
string,
count=0,
flags=0)Like sub but return a tuple (new_string, number_of_subs_made)re.escape(
pattern)Escape special characters in pattern
re.purge()Clear the regular expression cache
template)Return the string obtained by doing backslash substitution on template, as done by the sub() methodMatch.group(
[group1,...])Returns one or more subgroups of the match. 1 Argument returns string and more arguments return a tuple.Match.__getitem__(
g)Access groups with m[0], m[1] ...Match.groups(
default=None)Return a tuple containing all the subgroups of the matchMatch.groupdict(
default=None)Return a dictionary containing all the named subgroups of the match, keyed by the subgroup name.Match.start(
[group]
Match.end(
[group])Return the indices of the start and end of the substring matched by groupMatch.span(
[group])For a match m, return the 2-tuple
string[,
pos[,
endpos]]
Raw String Notation
In raw string notationr'text'
there is no need to escape the backslash character again.>>> re.match(r'W(.)1W', ' ff ')
>>> re.match('W(.)1W', ' ff ')
Reference
https://docs.python.org/3/howto/regex.htmlhttps://docs.python.org/3/library/re.htmlExtensions
(?...)This is the start of an extension(?aiLmsux)The letters set the correspondig flags See flags(?:...)A non-capturing version of regular parantheses(?P...)Like regular paranthes but with a named group(?P=name)A backreference to a named group(?#...)A comment(?=...)lookahead assertion: Matches if ... matches next without consuming the string(?!...)negative lookahead assertion: Matches if ... doesn't match next(?<=....)positive lookbehind assertion: Match if the current position in the string is preceded by a match for ... that ends the current position(?<!...)negative lookbehind assertion: Match if the current position in the string is not preceded by a match for ...(?(id/name)yes-pattern|no-pattern)Match with yes-pattern if the group with gived id or name exists and with no-pattern if notMatch objects
Match.expand(template)Return the string obtained by doing backslash substitution on template, as done by the sub() methodMatch.group(
[group1,...])Returns one or more subgroups of the match. 1 Argument returns string and more arguments return a tuple.Match.__getitem__(
g)Access groups with m[0], m[1] ...Match.groups(
default=None)Return a tuple containing all the subgroups of the matchMatch.groupdict(
default=None)Return a dictionary containing all the named subgroups of the match, keyed by the subgroup name.Match.start(
Regex Cheat Sheet Pdf
[group]
Match.end(
[group])Return the indices of the start and end of the substring matched by groupMatch.span(
[group])For a match m, return the 2-tuple
(m.start(group) m.end(group))
Match.posThe value of pos which was passed to the search() or match() method of the regex objectMatch.endposLikewise but the value of endposMatch.lastindexThe integer index of the last matched capturing group, or None
.Match.lastgroupThe name of the last matched capturing group or None
Match.reThe regular expression object whose match() or search() method produced this match instanceMatch.stringThe string passed to match() or search()Special escape characters
AMatch only at the start of the stringbMatch the empty string at the beginning or end of a wordBMatch the empty string when not at the beginning or end of a worddMatch any Unicode decimal digit this includes [0-9]DMatch any character which is not a decimal digitsMatch Unicode white space characters which includes [ tnrfv]SMatches any character which is not a whitespace character. The opposite of swMatch Unicode word characters including [a-zA-Z0-9_]WMatch the opposite of wZMatch only at the end of a stringRegular Expression Objects
Pattern.search(string[,
pos[,
endpos]]
)See
string[,
pos[,
endpos]])Likewise but see
string[,
pos[,
endpos]])Likewise but see
string,
maxsplit=0)Identical to
re.search()
. pos gives an index where to start the search. endpos limits how far the string will be searched.Pattern.match(string[,
pos[,
endpos]])Likewise but see
re.match()
Pattern.fullmatch(string[,
pos[,
endpos]])Likewise but see
re.fullmatch()
Pattern.split(string,
maxsplit=0)Identical to
re.split()
Pattern.findall(
pattern,
repl,
string,
count=0,
flags=0)Like sub but return a tuple (new_string, number_of_subs_made)re.escape(
pattern)Escape special characters in patternre.purge()Clear the regular expression cache
template)Return the string obtained by doing backslash substitution on template, as done by the sub() methodMatch.group(
[group1,...])Returns one or more subgroups of the match. 1 Argument returns string and more arguments return a tuple.Match.__getitem__(
g)Access groups with m[0], m[1] ...Match.groups(
default=None)Return a tuple containing all the subgroups of the matchMatch.groupdict(
default=None)Return a dictionary containing all the named subgroups of the match, keyed by the subgroup name.Match.start(
[group]
Match.end(
[group])Return the indices of the start and end of the substring matched by groupMatch.span(
[group])For a match m, return the 2-tuple
string[,
pos[,
endpos]])See
string[,
pos[,
endpos]])Likewise but see
string[,
pos[,
endpos]])Likewise but see
string,
maxsplit=0)Identical to
string[,
pos[,
endpos]])Similar to
string[,
pos[,
endpos]])Similar to
repl,
string,
count=0)Identical to
repl,
string,
count=0)Identical to
This is useful if you wish to include the flags as part of the regular expression, instead of passing a flag argument to the re.compile() function
pattern,
repl,
string,
count=0,
flags=0)Like sub but return a tuple (new_string, number_of_subs_made)re.escape(
pattern)Escape special characters in patternre.purge()Clear the regular expression cache
Raw String Notation
In raw string notationr'text'
there is no need to escape the backslash character again.>>> re.match(r'W(.)1W', ' ff ')
>>> re.match('W(.)1W', ' ff ')
Reference
https://docs.python.org/3/howto/regex.htmlhttps://docs.python.org/3/library/re.htmlExtensions
(?...)This is the start of an extension(?aiLmsux)The letters set the correspondig flags See flags(?:...)A non-capturing version of regular parantheses(?P...)Like regular paranthes but with a named group(?P=name)A backreference to a named group(?#...)A comment(?=...)lookahead assertion: Matches if ... matches next without consuming the string(?!...)negative lookahead assertion: Matches if ... doesn't match next(?<=....)positive lookbehind assertion: Match if the current position in the string is preceded by a match for ... that ends the current position(?<!...)negative lookbehind assertion: Match if the current position in the string is not preceded by a match for ...(?(id/name)yes-pattern|no-pattern)Match with yes-pattern if the group with gived id or name exists and with no-pattern if notMatch objects
Match.expand(template)Return the string obtained by doing backslash substitution on template, as done by the sub() methodMatch.group(
[group1,...])Returns one or more subgroups of the match. 1 Argument returns string and more arguments return a tuple.Match.__getitem__(
g)Access groups with m[0], m[1] ...Match.groups(
default=None)Return a tuple containing all the subgroups of the matchMatch.groupdict(
default=None)Return a dictionary containing all the named subgroups of the match, keyed by the subgroup name.Match.start(
Regex Cheat Sheet Pdf
[group]
Match.end(
[group])Return the indices of the start and end of the substring matched by groupMatch.span(
[group])For a match m, return the 2-tuple
(m.start(group) m.end(group))
Match.posThe value of pos which was passed to the search() or match() method of the regex objectMatch.endposLikewise but the value of endposMatch.lastindexThe integer index of the last matched capturing group, or None
.Match.lastgroupThe name of the last matched capturing group or None
Match.reThe regular expression object whose match() or search() method produced this match instanceMatch.stringThe string passed to match() or search()Special escape characters
AMatch only at the start of the stringbMatch the empty string at the beginning or end of a wordBMatch the empty string when not at the beginning or end of a worddMatch any Unicode decimal digit this includes [0-9]DMatch any character which is not a decimal digitsMatch Unicode white space characters which includes [ tnrfv]SMatches any character which is not a whitespace character. The opposite of swMatch Unicode word characters including [a-zA-Z0-9_]WMatch the opposite of wZMatch only at the end of a stringRegular Expression Objects
Pattern.search(string[,
pos[,
endpos]])See
re.search()
. pos gives an index where to start the search. endpos limits how far the string will be searched.Pattern.match(string[,
pos[,
endpos]])Likewise but see
re.match()
Pattern.fullmatch(string[,
pos[,
endpos]])Likewise but see
re.fullmatch()
Pattern.split(string,
maxsplit=0)Identical to
re.split()
Pattern.findall(string[,
pos[,
endpos]])Similar to
re.findall()
but with additional parameters pos and endposPattern.finditer(string[,
pos[,
endpos]])Similar to
re.finditer()
but with additional parameters pos and endposPattern.sub(repl,
string,
count=0)Identical to
re.sub()
Pattern.subn(repl,
string,
count=0)Identical to
re.subn()
Pattern.flagsThe regex matching flags.Pattern.groupsThe number of capturing groups in the patternPattern.groupindexA dictionary mapping any symbolic group names to group membersPattern.patternThe pattern string from which the pattern object was compiledThese objects are returned by the re.compile()
methodFlags
ASCII, AASCII-only matching in w, b, s and dIGNORECASE, Iignore caseLOCALE, Ldo a local-aware matchMULTILINE, Mmultiline matching, affecting^
and $
DOTALL, Sdot matches alluunicode matching (just in (?aiLmsux))VERBOSE, XverboseFlags are used in (?aiLmsux-imsx:...) or (?aiLmsux) or can be accessed with re.FLAG. In the first form flags are set or removed.This is useful if you wish to include the flags as part of the regular expression, instead of passing a flag argument to the re.compile() function