Friday, July 19, 2019

Python - Match vs Search


Regular Expression are very useful and can be used by developers in numerous of ways. It proved handy when we are encountering with the data having some sort of patterns. There are numerous of operation that can be done using regular expression. The re module can be used to import the regular expression module.

Match Operation in Python :-   Python match operation will look for the first matching string in a given set of data. If the searched data is not available at the first position, then it will return NONE.

Example :-  We will check what will be the output when we look for the data set that is available at the first place and when it is not at the first index position.



Python Code :- (when at the first position)
import re
data = "this is good"
res_match = re.match("this",data)
print("The result set when it is at first place",res_match)

Output :-
The result set when it is at first place <re.Match object; span=(0, 4), match='this'>

Python Code :- (when not at first position)
import re
data = "this is good"
res_mat_pos = re.match("good",data)
print("The result set when it is not in first place",res_mat_pos)
Output :- 
The result set when it is not in first place None.

Search Operation in Python :- Search operation works in the similar way as match works , except that it can search the word that are in different index position.

Sample Python Code :-
import re
data = "this is good"
result = re.search("good",data)
print("This is the result for the search",result)
Output :-
This is the result for the search <re.Match object; span=(2, 4), match='is'>

Thanks !!!

No comments:

Post a Comment

Hadoop - What is a Job in Hadoop ?

In the field of computer science , a job just means a piece of program and the same rule applies to the Hadoop ecosystem as wel...