Show Menu
Cheatography

PYTHON REGULAR EXPRESSION CHEATSHEET Cheat Sheet by

Regular expressions in one of the important topic in python. It is widely used by developer and testers. If you want to play around with data. One should know regular expressions.

What you want do with regular expres­sion?

Regular expres­sions can be used to check if a string contains the specified search pattern.
Regular expres­sions are used to remove the unnece­ssary words
It also helps in finding out the correc­tness of the data and even operations such as finding, replacing and formatting the data is possible using Regular Expres­sions

Group/­Ranges

.
any single character
(a|b)
a or b
[abc]
Range (a or b or c)
[^abc]
Not (a or b or c)
[a-z]
Lower case letter from a to z
[A-Z] Upper case letter from A to Z
Upper case letter from A to Z
[0-10]
Digit from 0 to10
\x
Group/­­su­b­p­attern number "­­x"

Escape Sequences

\
Escape following character
\Q
Begin literal sequence
\E
End literal sequence

Assertions

?=
Lookahead assertion
?!
Negative lookahead
?<=
Lookbehind assertion
?!= or ?<!
Negative lookbehind
?>
Once-only Subexp­­re­ssion
?()
Condition [if then]
?()|
Condition [if then else]
?#
Comment

USE

import re
string = "­"­"I am Sourabh and my mobile no is 947986­402­6"""
regex = '\d+'
match = re.fin­dal­l(r­egex, string)
print(­match)
 

Anchors

^
Start of string, or start of line in multi-line pattern
\A
Start of string
$
End of string, or end of line in multi-line pattern
\Z
End of string
\b
Word boundary
\B
Not word boundary
\<
Start of word
\>
End of word

Characters

\c
Control character
\s
White space
\S
Not white space
\d
Digit
\D
Not digit
\w
Word
\W
Not word
\x
Hexade­­cimal digit
\O
Octal digit

Pattern Modifiers

g
Global match
i *
Case-i­­ns­e­n­sitive
m *
Multiple lines
s *
Treat string as single line
x *
Allow comments and whitespace in pattern
e *
Evaluate replac­­ement
U *
Ungreedy pattern

USE

import re
string = 'hello 12 hi 89. Its me 343'
pattern = '\d+'
result = re.fin­dal­l(p­attern, string)
print(­result)

output­#12­89343

USE

import re

phone = "MY phone is +91-9479-86-40-26r"

# Remove anything other than digits
num = re.sub(r'\D', "", phone)    
print ("Phone Num : ", num)

output Phone num : 9479864026
 

Quanti­fiers

*
0 or more
{5}
exactly 5
+
1 or more
{5,}
5 or more
?
0 or 1
{1,5}
1,2,3,4 or 5

Special Characters

\n
New line
\r
Carriage return
\t
Tab
\v
Vertical tab
\f
Form feed
\xxx
Octal character xxx
\xhh
Hex character hh

String Manipu­lation

$`
Before matched string
$'
After matched string
+
Last matched string
$&
Entire matched string
import repattern = '^a...s$' test_s­tring = 'abyss' result = re.mat­ch(­pat­tern, test_s­tring) if result: print(­"­Search succes­sfu­l.") else: print(­"­Search unsucc­ess­ful.")

String Manipu­lation

$`
Before matched string
$'
After matched string
+
Last matched string
$&
Entire matched string

posix

[:upper:]
Upper case letters
[A-Z]
[:lower:]
lower case letter
[a-z]
[:alpha:]
alphabets
[a-zA-Z]
[:alnum:]
alphan­umeric
[a-zA-­z0-9]
[:digit:]
Digits
[0-9]
[:xdigit:]
Hexade­cimal
[A-Fa-­f0-9]
[:punct:]
Punctu­­ation
[!"­\#$%­&'­()*+, \-./:;­<=>?@\[ \\\]^_­‘{|}~]
[:blank:]
Space and tab
[:space:]
Blank characters
[:cntrl:]
Control characters
[:graph:]
Printed characters
[:print:]
Printed characters and spaces
[:word:]
Digits, letters and underscore
 

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

          More Cheat Sheets by sahusourabh

          FUNCTIONS IN PYTHON Cheat Sheet
          ITERATORS GENERATORS DECORATORS Cheat Sheet