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. 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 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)
text = 'my cat and your cats are pretty'
re.search(pattern, text)