Python Regex Cheatsheet

Posted : admin On 1/3/2022
Python Regex Cheatsheet

Python Regex Cheat Sheet

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. 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. Python Regex Cheatsheet. Regular Expression Basics. Any character except newline: a: The character a: ab: The string ab: a b: a or b: a.: 0 or more a's Escapes a.

Regex Syntax¶

Characters
CharacterMatches
aa character
.Any character (except newline)
.. character
character
** character
Character Classes
MatchesDescription
[abcd]Any one of the letters a through dSet of characters
[^abcd]Any character but a, b, c, or dComplement of a set of characters
[a-d]Any one of the letters a through dRange of characters
[a-dz]Any of a, b, c, d, or zRange of characters
Special Sequences
TypeExpressionEquivalent ToDescription
Word Characterw[a-zA-Z0-9_]Alphanumeric or underscore
Non-word CharacterW[^a-zA-Z0-9_]Anything but a word character
Digit Characterd[0-9]Numeric
Non-digit CharacterD[^0-9]Non-numeric
Whitespace Characters[tnrfv]Whitespace
Non-whitespace CharacterS[^tnrfv]Anything but a whitespace character
Anchors
AnchorMatches
^Start of the string
$End of the string
bBoundary between word and non-word characters
Groups
Group TypeExpression
Capturing( ... )
Non-capturing(?: ... )
Quantifiers/Repetition
QuantifierModification
{5}Match expression exactly 5 times
{2,5}Match expression 2 to 5 times
{2,}Match expression 2 or more times
{,5}Match expression 0 to 5 times
*Match expression 0 or more times
{,}Match expression 0 or more times
?Match expression 0 or 1 times
{0,1}Match expression 0 or 1 times
+Match expression 1 or more times
{1,}Match expression 1 or more times
Non-greedy quantifiers
QuantifierModification
{2,5}?Match 2 to 5 times (less preferred)
{2,}?Match 2 or more times (less preferred)
{,5}?Match 0 to 5 times (less preferred)
*?Match 0 or more times (less preferred)
{,}?Match 0 or more times (less preferred)
??Match 0 or 1 times (less preferred)
{0,1}?Match 0 or 1 times (less preferred)
+?Match 1 or more times (less preferred)
{1,}?Match 1 or more times (less preferred)
Alternators
QuantifierModification
ABC DEFMatch string ABC or string DEF
Lookaround
QuantifierModification
(?=abc)Zero-width match confirming abc will match upcoming chars
(?!abc)Zero-width match confirming abc will not match upcoming chars

Python¶

functions
FunctionPurposeUsage
re.searchReturn a match object if pattern found in stringre.search(r'[pat]tern','string')
re.finditerReturn an iterable of match objects (one for each match)re.finditer(r'[pat]tern','string')
re.findallReturn a list of all matched strings (different when capture groups)re.findall(r'[pat]tern','string')
re.splitSplit string by regex delimeter & return string listre.split(r'[-]','st-ring')
re.compileCompile a regular expression pattern for later usere.compile(r'[pat]tern')
flags
FlagDescription
re.IGNORECASEMatch uppercase and lowercase characters interchangeably
re.VERBOSEIgnore whitespace characters and allow # comments

I send out 1 Python exercise every week through a Python skill-building service called Python Morsels.

If you'd like to improve your Python skills every week, sign up!

You can find the Privacy Policy here.

How To Use Regex In Python

Sheet

Python Regex Cheat Sheet Pdf

reCAPTCHA protected (Google Privacy Policy & TOS)