Skip to main content

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
Submitted by devanshi.srivastava on July 16, 2021

Devanshi, is working as a Data Scientist with iVagus. She has expertise in Python, NumPy, Pandas and other data science technologies.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.