Python RegEx search() Functions

This function of re in Python will search the regular expression pattern and return the first occurrence. The Python RegEx Match method checks for a match only at the beginning of the string. So, if a match is found in the first line, it returns the match object. But if a match is found in some other line, the Python RegEx Match function returns null.

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.

Syntax:

>>>match = re.search(pattern, str)

Example:

import re

str = "Hello! Welcome to Python Programming"
res=re.search("to",str)
print("The give character is located in position:",(res.start()),"-",(res.end()))

Output: The give character is located in position: 15 - 17

Tags