WORKING WITH PYTHON REGULAR EXPRESSIONS
WHY REGULAR EXPRESSIONS?
#Example1
str1="My Name is Rajesh"
print([Link]("Rajesh","MSD_7"))
#Example2:
str2="Main Street is broad road"
print([Link]("road","rd"))
NOTE:
The above example converting all "road" patterns into rd, this is
illegal, that time we are counting characters as follows...!!
#Example3: We can replace with the help of index
print(str2[0:17]+str2[17:].replace("road","rd"))
NOTE:
The above example display result as per programmer expectation, but
counting characters every time is big challange, that time we are
implementing PYTHON regexp or regex or re.
Define re?
An Expression, which is matching multiple conditions.
OR
Regular Expressions are powerful standardized way of searching,
replacing, and parsing text with complex patterns of characters.
Syntax
import re
Regular
Expressio
n
^ Matches beginning of line.
Patterns
$ Matches
You can escapeend of [Link] by preceding it with a backslash.
a control
. Matches
Pattern any single character except newline.
Description
re* Matches 0 or more occurrences of preceding expression.
re+ Matches 1 or more occurrence of preceding expression.
re? Matches 0 or 1 occurrence of preceding expression.
Special Character Classes
Example Description
\d Match a digit: [0-9]
\D Match a nondigit: [^0-9]
\s Match a whitespace
\S character: [ \t\r\n\f]
\w Match nonwhitespace:
\W [^ \t\r\n\f]
Match a single word character: [A-Za-z0-9_]
Match a nonword character: [^A-Za-z0-9_]
Literal characters
Example Description
[Pp]ython Match "Python" or "python"
rub[ye] Match "ruby" or "rube"
[0-9] Match any digit; same
[^0-9] as[0123456789]
[a-z] Match anything other than a digit
[A-Z] Match any lowercase
[a-z A-Z Match any uppercase
0-9] Match any of the above
Repetition Cases
Example Description
ruby? Match "rub" or "ruby": the y is optional
ruby* Match "rub" plus 0 or more y's
ruby+ Match "rub" plus 1 or more y's
\d{3} Match exactly 3 digits
\d{3,} Match 3 or more digits
\d{3,5} Match 3, 4, or 5 digits
The most common uses of Regular Expressions are:
1 Search a string (match & search)
2 Finding a string (findall)
3 Break string into a sub strings (split)
4 Replace part of a string (sub)
Various methods of RE?
The 're' package provides multiple methods to perform queries on an
input string.
1 [Link]() 2 [Link]()
3 [Link]() 4 [Link]() 5 [Link]()
The match Method
It finds and match, if pattern occurs at start of the string.
Syntax:
[Link](pattern, string)
Example:
import re
line="pet:cat I love cats"
mat=[Link](r"pet:\w\w\w",line)
print(mat)
NOTE:
It shows that pattern match has been found. To print the matching
string use method group, It helps to return the matching string.
import re
line="pet:cat I love cats"
mat=[Link](r"pet:\w\w\w",line)
print([Link](0))
Example: With Multiple Patterns
import re
line="Pet:Cat I like Pets Pet:Cow I love Cows"
mat=[Link](r"Pet:\w\w\w",line)
print(mat)
O/P:
<_sre.SRE_Match object; span=(0, 7),
match='Pet:Cat'>
Example:With Multiple Groups
import re
line="Pet:Cat I like Pets
Pet:Cow I love Cows"
mat=[Link](r"Pet:\w\w\w",line)
print([Link](0)) #Pet:Cat
print([Link](1)) #Index Error no such group
Example:Using start and end methods
import re
line="Pet:Cat I like Pets Pet:Cow I
love Cows"
mat=[Link](r"Pet:\w\w\w",line)
print([Link](0)) #Pet:Cat
print([Link]())#0
print([Link]())#7
NOTE: r always indicates PYTHON
raw string..!!
NOTE: match method only matches
the patterns in the starting..!!
The search Method
It searches for first occurrence
of RE pattern within string.
Syntax:
[Link](pattern, string)
Example:
import re
line="pet:cat I love cats"
mat=[Link](r"pet:\w\w\w",line)
print([Link](0))
NOTE:
match() function matches starting of the string, search() function
search in entire the string..!!
Example:
import re
line="I love cats pet:cat"
mat=[Link](r"pet:\w\w\w",line)
print(mat)
Example:
import re
line="I love cats pet:cat"
mat=[Link](r"pet:\w\w\w",line)
print([Link](0))
Example:
import re
line=" pet:cat I love cats pet:cow I love cow"
mat=[Link](r"pet:\w\w\w",line)
print([Link](0))
NOTE:
search method returns only first occurance of the pattern in the
string or line. If we need all matching patterns we should use
findall().
[Link] Method:
It helps to get a list of all matching patterns. It has no
constraints of searching from start or end. If we will use method
findall to search cat in given string it will return both occurrence
of cow.
Syntax:
[Link] (pattern, string)
Example:
import re
line=" pet:cat I love cats pet:cow I love cow"
mat=[Link](r"pet:\w\w\w",line)
print(mat)
Example:
import re
result=[Link](r'@\w+.\w+','[Link]@[Link], xyz@[Link],
[Link]@[Link], [Link]@[Link]')
print(result)
Example:
import re
result=[Link](r'@\w+.(\w+)','[Link]@[Link], xyz@[Link],
[Link]@[Link], [Link]@[Link]')
print(result)
[Link] Method:
It helps to split string by the occurrences of given pattern.
Syntax:
[Link](pattern,string)
Example:
import re
line="I love cats pet:cat I love cows, pet:cow thank U"
mat=[Link](r"pet:\w\w\w",line)
print(mat)
Example:
import re
print([Link](r'\s','TATA CONSULTANCY SERVICES'))
print([Link](r'\s','Leader in IT Sector Sevices'))
Example:
import re
result=[Link](r' ','TCS Bangalore')
print(result)
Syntax2:
[Link](pattern, string,
[maxsplit=0])
Example:
import re
result=[Link](r'a','TCS Bangalore',maxsplit=1)
print(result)
[Link]():
It helps to search a pattern and replace with a new sub string. If
the pattern is not found, string is returned unchanged.
Syntax:
[Link](pattern, repl, string):
Example:
import re
str1="raj@[Link] and ksr@[Link] and vara@[Link]"
mat=[Link](r"@\w+","@gmail",str1)
print(mat)
Example:
import re
text = "Python for beginners is a cool Scripting"
pattern = [Link]("cool", "good", text)
print(pattern)
*************************************************
*******************
*************
Example:
import re
result = [Link](r'BigData', 'Data Science Based on BigData')
print ([Link](0))
#group(0) didn't return the entire match
Example:
import re
fi = open('[Link]')
for line in fi:
if
[Link]('^F
', line) :
print(line)
rstrip()
It returns a copy of the string in which all chars have been
stripped from the end of the string.
Syntax
[Link]([chars])
Example:
import re
fi =
open('Hai
.txt')
for line in fi:
if [Link]('From:', line) :
line = [Link]()
print(line)
Search and Replace
One of the most important re methods that use regular expressions is
sub and search.
Syntax
[Link](pattern, repl)
Example:
import re
s = 'TCSOn ITComplex
MAIN ROAD..!!'
Rep=[Link]('TCSOn', 'TCS:')
print(Rep)
[Link] Method:
It helps to get a list of all matching patterns. It has no
constraints of searching from start or end. If we will use method
findall to search NiT in given string it will return both occurrence
of nit.
Syntax:
[Link] (pattern, string)
Example: Extract each character (using "\w")
import re
result=[Link](r'.', 'TATA CONSULTANCY
SERVICES')
print(result)
result1=[Link](r'\w','TATA CONSULTANCY SERVICES')
print(result1)
Extract each word (using "*" or "+")
Example:
import re
result=[Link](r'\w*', 'TATA CONSULTANCY SERVICES Leader in IT
Services')
print (result)
Example:
import re
result=[Link](r'\w+', 'TATA CONSULTANCY SERVICES Leader in IT
Services')
print (result)
Extract each word (using "^")
Example:
import re
result=[Link](r'^\w+','TATA CONSULTANCY SERVICES')
print (result)
Example:
import re
result=[Link](r'\w+$', 'TATA CONSULTANCY SERVICES Leader in IT
Services')
print (result)
Return the first three character of each word (\w)
Example:
import re
result=[Link](r'\w\w\w', 'TATA CONSULTANCY SERVICES Leader in IT
Services')
print (result)
Extract consecutive two characters those available at start of word
boundary (using "\b")
Example:
import re
result=[Link](r'\b\w.', 'TATA CONSULTANCY SERVICES Leader in
IT')
print (result)
Return the domain type of given email-ids
Extract all characters after “@”
Example:
import re
result=[Link](r'@\w+',"""[Link]@[Link], xyz@[Link],
[Link]@[Link], [Link]@[Link]""")
print (result)
NOTE: Above, you can see that ".com", ".in" part is not
extracted.
Example:
import re
result=[Link](r'@\w+.\w+',"""[Link]@[Link], xyz@[Link],
[Link]@[Link], [Link]@[Link]""")
print (result)
Example: Extract only domain name using "( )"
import re
result=[Link](r'@\w+.(\
w+)',"""[Link]@[Link],
xyz@[Link],
[Link]@[Link], [Link]@[Link]""")
print (result)
Return date from given string: Here we will use "\d" to extract
digit.
Example:
import re
result=[Link](r'\d{2}-\d{2}-\d{4}',"""Amit 34-3456 12-05-
2007,
XYZ 56-4532 11-11-2011, ABC 67-8945 12-01-2009""")
print (result)
NOTE: If you want to extract only year again parenthesis "( )" will
help you.
Example:
import re
result=[Link](r'\d{2}-\d{2}-(\d{4})',"""Amit 34-3456 12-05-2007,
XYZ 56-4532 11-11-2011, ABC 67-8945 12-01-2009""")
print (result)
[Link] Method:
"s": This expression is used for creating a space in the string
Example:
import re
print([Link](r'\s','TATA CONSULTANCY SERVICES'))
print([Link](r'\s','Leader in IT Services'))
Example: Split a string with multiple delimiters
import re
line = 'asdf fjdk;afed,fjek,asdf,foo'
result= [Link](r'[;,\s]', line)
print(result)
NOTE: We can also use method [Link]() to replace these multiple
delimiters with one as space.
[Link](pattern, repl, string):
It helps to search a pattern and replace with a new sub string. If
the pattern is not found, string is returned unchanged.
Example:
import re
result=[Link](r'India','the World','NiT is largest Training Center\
of India')
print(result)
Example:
import re
line = 'asdf fjdk;afed,fjek,asdf,foo'
result= [Link](r'[;,\s]',' ', line)
print(result)
Examples::
import re
PyLine="Pet:Cat I like Pets"
PyMat=[Link](r"Pet:\w\w\w",PyLine)
print(PyMat)
#<[Link] object; span=(0, 7), match='Pet:Cat'>
#It shows that pattern has been found
#to display pattern we should use group()
print([Link]())
import re
PyLine="Pet:Cat I like Pets"
PyMat=[Link](r"Pet:\w\w\w",PyLine)
print(PyMat)
#<[Link] object; span=(0, 7), match='Pet:Cat'>
#It shows that pattern has been found
#to display pattern we should use group()
print([Link]())
print([Link]())
print([Link]())
import re
PyLine="Pet:Cat I like Pets"
PyMat=[Link](r"Pet:\w\w\w",PyLine)
print(PyMat)
#<[Link] object; span=(0, 7),
match='Pet:Cat'>
#It shows that pattern has been
found
#to display pattern we should use group()
print([Link]())
print([Link]())
print([Link]())
#r means raw string ==> ASCII Formated data
#ASCII means English only
#u means unicode ==> Every character not
only English
import re
PyLine="I like Pets Pet:Cat"
PyMat=[Link](r"Pet:\w\w\w",PyLine)
print([Link]())
#AttributeError: 'NoneType' object has no attribute 'group'
#It finds and match,
#if pattern occurs at start of the string.
import re
PyLine="I like Pets Pet:Cat"
PyMat=[Link](r"Pet:\w\w\w",PyLine)
print([Link]())
PyLine="Pet:Cat I like Pets"
PyMat=[Link](r"Pet:\w\w\w",PyLine)
print([Link]())
PyLine="Pet:Cat I like Pets Pet:Cow I
love Cows"
PyMat=[Link](r"Pet:\w\w\w",PyLine)
print([Link](0))
import re
PyLine="I like Pets Pet:Cat"
PyMat=[Link](r"Pet:\w\w\w",PyLine)
print([Link]())
PyLine="Pet:Cat I like Pets"
PyMat=[Link](r"Pet:\w\w\w",PyLine)
print([Link]())
PyLine="Pet:Cat I like Pets Pet:Cow I
love Cows"
PyMat=[Link](r"Pet:\w\w\w",PyLine)
print([Link](0))
print([Link](1))#IndexError: no such group
#First occurrence of RE pattern within string.
import re
PyLine="I like Pets Pet:Cat"
PyMat=[Link](r"Pet:\w\w\w",PyLine)
print([Link]())
PyLine="Pet:Cat I like Pets"
PyMat=[Link](r"Pet:\w\w\w",PyLine)
print([Link]())
PyLine="Pet:Cat I like Pets Pet:Cow I love Cows \
Pet:Cat I like Pets Pet:Cow I love Cows \
Pet:Cat I like Pets Pet:Cow I love Cows"
PyMat=[Link](r"Pet:\w\w\w",PyLine)
print(PyMat)
import re
PyLine="I like Pets Pet:Cat"
PySp=[Link](r"Pet:\w\w\w",PyLine)
print(PySp)
PyLine="Pet:Cat I like Pets"
PySp=[Link](r"Pet:\w\w\w",PyLine)
print(PySp) print([Link](r'\
s',PyLine))
#\s white space character
#space replaced with , comma separator
print([Link](r'\S',PyLine))
#Every Character replaced with a comma
separator
import re
PyMails="raj@[Link], ksraju@[Link], nit@[Link]\
raj@[Link], ksraj@[Link], nit@[Link]"
print(PyMails)
#raj@[Link], ksraju@[Link],
nit@[Link]@[Link],
ksraj@[Link], nit@[Link]
PySub=[Link](r"@\w+","@gmail",PyMails)
print(PySub)
#raj@[Link], ksraju@[Link], nit@[Link]@[Link],
ksraj@[Link], nit@[Link]