Prerequisite: Regex in Python
Use of re.search() and re.match() –
re.match() both are functions of
re module in python. These function are very efficient and fast for searching in strings. The function searches for some substring in a string and return a match object if found, else it returns none.
re.search() vs re.match() –
There is a difference between the use of both functions. Both return first match of a substring found in the string, but
re.match() searches only in the first line of the string and return match object if found, else return none. But if a match of substring is found in some other line other than the first line of string (in case of a multi-line string), it returns none.
re.search() searches for the whole string even if the string contains multi-lines and tries to find a match of the substring in all the lines of string.
<_sre.SRE_Match object; span=(69, 75), match='string'> None
re.search()is returning match object and implies that first match found at index 69.
re.match()is returning none because match exist in second line of the string and re.match() searches only in first line of string.
re.IGNORECASEis used to ignore the case sensitivity in the strings.
re.match()returns only first occurrence of substring in string and ignore others.
- Important differences between Python 2.x and Python 3.x with examples
- Reading Python File-Like Objects from C | Python
- Python | Convert list to Python array
- Python | Merge Python key values to list
- Python | Index of Non-Zero elements in Python list
- MySQL-Connector-Python module in Python
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- Python | Add Logging to a Python Script
- Python | Add Logging to Python Libraries
- Python | Visualizing O(n) using Python
- gcd() in Python
- zip() in Python
- SQL using Python | Set 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.