Search():
- match() checks only at the beginning of the string while search() checks for a match anywhere in the string.
- Here you can see that, search() method is able to find a pattern from any position of the string but it only returns the first occurrence of the search pattern.
import re
line = "Java and Python"
res = re.search(r'abc' , line)
print("Failure :",res)
res = re.search(r'd P' , line)
print("Success :",res)
- group(num=0): This method returns entire match
- groups(): This method returns all matching subgroups in a tuple
import re
line = "Java plus Python gives Jython"
found = re.search( r'ython', line)
if found:
print("Found : ", res.group(0))
else:
print("Not Found!")
- There are methods like start() and end() to know the start and end position of matching pattern in the string.
import re
line = "Java plus Python gives Jython"
res = re.search( r'Python', line)
print("Start : ", res.start())
print("End : ", res.end())