Previous Post :
Python #21-Regex in Python
Next Post :
Python #23-Regex findall() and substring() methods in Python
In Python re module consists of regular expression methods.
Python #21-Regex in Python
Next Post :
Python #23-Regex findall() and substring() methods in Python
Regex methods in Python
In Python re module consists of regular expression methods.
- re.match()
- re.search()
- re.findall()
re.match() :
- This method finds match if it occurs at starting position of the string.
For eg: calling match() on the string "Google Bird" and looking for "Google" will match. In case if your are going to look for Bird it will not match because match() will only recognize only the starting position of the string.
Syntax:
re.match(pattern, string)
Source Code:
import re
variable = re.match(r"Google", "Google Bird")
print variable.group(0)
Note Worthy: Here in Line 2: "Google" is pattern which i want to search. "Google Bird" is the string as per the Syntax...So don't confuse it..
Output:
Google
In case if you are going to search Bird in the given string even though the word is present its shows you none..Because calling match() will only consider the beginning of the string not the entire string.
re.search():
- It scan through a string, looking for any location in the string. It is similar to match() but it gives some extra bit surprise for us search() will check entire string whether the given string is presnt or not where match() only checks with starting of the string.
Syntax:
re.search(pattern, string)
Source Code:
import re
variable = re.search(r"Bird", "Google Bird DD")
print variable.group(0)
Output:
Bird
Comments