examples/regex/search.py
from __future__ import print_function import re match = re.search(r'lac', 'The black cat climed') if match: print("Matching") # Matching print(match.group(0)) # lac match = re.search(r'dog', 'The black cat climed') if match: print("Matching") else: print("Did NOT match") print(match) # None
The search method returns an object or None, if it could not find any match. If there is a match you can call the group() method. Passing 0 to it will return the actual substring that was matched.