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





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())



Output


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



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

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250