Python re.search() Function: A Tutorial

0
4582
Python re search Example | re.search() in Python

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.

Python re.search()

Python re.search() is a built-in regex method that searches the string for a match and returns the match object if there is a match. If no matches are found, None is returned.

The re.search() function takes two parameters and returns a match object if there is a match.

If there is more than one match, only the first occurrence of the match will be returned. 

Syntax

re.search(pattern, string)

Parameters

The re.search() function takes two required parameters.

  1. pattern: It is the pattern that we have to check against the string.
  2. string: It is a string in which we find the pattern.

Return Value

The search() function returns a Match object if there is a match.

Example

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())

Output

The first white-space character is located in position: 6

We have an initialized txt variable with Millie Finn Gaten value in this example. Remember that string starts with 0 indexes.

We find the first white space character in the string; if it finds it, 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)

Output

The first white-space character is located in position: None

There is no next-line character in this example, so it returns None.

Python re.search() vs re.match()

There is a difference between using both Python re.search() and re.match() functions.

Both functions return the first match of a substring found in the string, but the re.match() method searches only in the first line of the string and returns a match object if found, else returns 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 the 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 the string.

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))

Output

<re.Match object; span=(12, 17), match='lived'>
None

The re.search() method returns a match object and implies that the first match is found at index 17.

The re.match() method returns None because the match exists in the second line of the string, and re.match() searches only in the first line.

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.

Conclusion

The search() method searches through a string looking for the first location where the regular expression pattern produces a match and returns 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.

See also

Python re.sub()

Python regex replace

Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.