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.
- How to write an empty function in Python - pass statement?
- Operator Functions in Python | Set 2
- Time Functions in Python | Set-2 (Date Manipulations)
- Send mail from your Gmail account using Python
- Python – The new generation Language
- Print Single and Multiple variable in Python
- Increment and Decrement Operators in Python
- str() vs repr() in Python
- Swap two variables in one line in C/C++, Python, PHP and Java
- Generate all permutation of a set in Python
- Class or Static Variables in Python
- trunc() in Python
- Division Operators in Python
- Interesting facts about strings in Python | Set 1
- When to use yield instead of return in Python?
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.