Python re search Example | re.search() in Python
Python re.search() is an inbuilt regex function that searches the string for a match and returns the match object if there is a match. If there is more than one match, only the first occurrence of the match will be returned. If no matches are found, the value None is returned.
A regular expression (or RE) specifies the set of strings that match it; the functions in the re module let you check if a particular string matches a given regular expression.
The re.search() function takes two required parameters.
- Pattern: It is the pattern which we have to check against the string.
- String: It is a string in which we find the pattern.
The search function returns a Match object if there is a match.
See the following function.
import re txt = "Millie Finn Gaten" sub_str = re.search("\s", txt) print("The first white-space character is located in position:", sub_str.start())
The first white-space character is located in position: 6
In this example, we have an initialized txt variable with Millie Finn Gaten value. Remember that string starts with 0 index.
Now, we find the first white space character in the string and if it finds then it will return the Match object.
Then we call the start() method on that object and it returns the index of that character.
In our example, the position of the white space character is 6. So, it returns six.
No matches found
If no matches are found, the value None is returned.
import re txt = "Millie Finn Gaten" sub_str = re.search("\n", txt) print("The first white-space character is located in position:", sub_str)
The first white-space character is located in position: None
In this example, there is no next line character so it returns None.
Python re.search() vs re.match()
There is a difference between the use of both Python re.search() and re.match() functions. Both functions return the first match of a substring found in the string, but re.match() method 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.
While re.search() method search 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.
See the following code example.
import re Substring = 'lived' String = '''The boy who lived come to die''' # Use of re.search() Method print(re.search(Substring, String, re.IGNORECASE)) # Use of re.match() Method print(re.match(Substring, String, re.IGNORECASE))
<re.Match object; span=(12, 17), match='lived'> None
The re.search() method is returning match object and implies that the first match found at index 17.
The re.match() method is returning None because match exists in the second line of the string and re.match() searches only in the first line of string.
The re.IGNORECASE is used to ignore the case sensitivity in the strings.
Both re.search() and re.match() method returns only the first occurrence of a substring in the string and ignores others.
The search() method searches through a string looking for the first location where the regular expression pattern produces a match, and return a corresponding match object.
It returns None if no position in the string matches the pattern; note that this is different from finding the zero-length match at some point in the string.
That is it for re.search() function.