Show Menu
Cheatography

Python Regular Expressions Cheat Sheet by

Regular Expressions that I have been taught.

Definition

Regular expression is a tool for matching patterns in text. With the help of regular expression you can find, match and replace text in strings.
A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings, using a specia­lized syntax held in a pattern.
Module name re is imported to be used.

White Space Characters

\n
Newline
\s
Space
\t
Tab
\e
Escape
\r
Return
 

Identi­fiers

\d
Any single character or number
\D
Anything but a number (non-digit character)
\s
Space
\S
Anything but a space
\w
Any character (A-Z, a-z, 0-9 and unders­core)
\W
Anything but a character
.
Any single character except for a new line
\b
The white space around words
\.
A period

Functions

match()
Syntax - re.mat­ch(­pat­tern, string, <fl­ag=­0>)
search()
For any pattern in the string - only the first match
findall()
Matches all occurr­ences
split()
Splits the string - forms an array
replace()
To replace one or more characters in a string
 

Modifiers

{x,y}
We expect x to y integers
+
Match one or more occurr­ences
?
Match 0 or 1 occurrence
*
Match 0 or more of the preceding expression (eg. x* will match occurr­ences of x)
$
Match the end of the string
^
Matching the beginning of a string
|
Used as OR
[]
Displaying a range
()
To group the regular expres­sions
   
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

            Python 3 Cheat Sheet by Finxter