- search() :
Syntax-
re.search(pattern,string)
match():
Syntax-
re.match(pattern,string,flag=0)
-Both of the function are used to match the pattern to the string in regular expression.
Difference-
match() function search the pattern only at the starting of the string.
while search() function search the pattern in whole string.
Example-
string is s="this is string"
re.match('is',s) will not match.
re.search('is',s) will match.
program-
#import the re module
import re
#Given string is
s = "This is programming language"
#search by match() function
x=re.match('is',s)
if x:
print("Matched by match() function")
else:
print("Not matched")
#search by search() function
x=re.search('is',s)
if x:
print("Matched by search() function")
else:
print("Not matched")
Not matched
Matched by search() function