import re
Introduction
def find(pattern, text):
= re.search(pattern, text)
match if match: return match.group()
else: print("Match not Found")
= "igs"
pattern = "called piiig"
text find(pattern, text)
Match not Found
"iig", "called piiig") find(
'iig'
"...g", "called piiig") find(
'iiig'
"...g", "called pimig") find(
'imig'
Repeating characters
- regex+ - one or more repetitions of preceding element
- regex* - zero or more repetitions of preceding element
- regex? – zero or one repetition of preceding element
- regex{n} - exactly n repetitions of preceding element
- regex{n,m} - from n to m repetitions of preceding element
- regex{n,} - n or more repetitions of preceding element
In Python you can use (.)\1{9,}
- (.) makes group from one char (any char)
- \1{9,} matches nine or more characters from 1st group
r"(.)\1{2}g", "called piiig") #\1 - refers to first group. () indicates the group find(
'iiig'
r"(.)\1{2}g", "called piig") find(
Match not Found
r"(.)\1{2}g", "called psssg") find(
'sssg'
r"(.)\1{2}g", "called pssssg") find(
'sssg'
r"(.)\1{1,}g", "called pssssg") find(
'ssssg'
r"(.)\1{1,}g", "called pssg") find(
'ssg'
r"(.)\1{1,}g", "called psg") find(
Match not Found
Finding Email
r"[\w.]+@[\w.]+", "My email is foo@bar blah blah rahul.sarafiitk@gmail.com") # How to ensure atleast 1 . in second word find(
'foo@bar'
r"[\w.]+@[\w.]+", "My email is rahul.sarafiitk@gmail.com blah blah foo@bar") find(
'rahul.sarafiitk@gmail.com'