What is the search() function in Python?

PythonServer Side ProgrammingProgramming

In Python, search() is a method of the module re.

Syntax of search()

re.search(pattern, string):

It is similar to re.match() but it doesn’t limit us to find matches at the beginning of the string only. Unlike in re.match() method, here searching for pattern ‘Tutorials’ in the string ‘TP Tutorials Point TP’ will return a match.

Example

import re
result = re.search(r'Tutorials', 'TP Tutorials Point TP')
print result.group(0)

Output

Tutorials

Here you can see that, search() method is able to find a pattern from any position of the string but it only returns the first occurrence of the search pattern.

raja
Published on 28-Dec-2017 09:52:57
Advertisements