re.search

Full name
re.search
Library
re
Syntax

re.search(pattern, string, flags=0)

Description

The re.search function traverses the string searching for the first match of the pattern, returning the corresponding match object.(link is external) In case of no match found, the function returns None.

Parameters
  • pattern: Search pattern.
  • string: Text in which to search.
  • flags: Search modifiers.
Result

The re.search function returns a match object(link is external) or None if no match is found.

Examples

We can search for the first occurrence of the texts 'cat' or 'cats' in the sentence 'my cat and your cats are pretty' with the following code:

pattern = r'(cat)s?'
text = 'my cat and your cats are pretty'
re.search(pattern, text)
<re.Match object; span=(3, 6), match='cat'>
Submitted by admin on Thu, 05/06/2021 - 07:29