Articles → Python → Search function in python

Search function in python






Purpose







Example


import re

pattern = "country"
text = "my country is India"

#returns Match object
match = re.search(pattern, text)


print(match)

Output


Picture showing the match object when country word is matched in the string

Click to Enlarge




Finding multiple instances




import re

pattern = "country"
text = "my country is India and I belong to this country"

for match in re.finditer(pattern, text):
    print(match.span())




Picture showing the span function of match object when multiple instances are matched

Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Friday, July 03, 2020

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250