❮ grep
❯
- r
- re
- search|re
- group|re
Regexes first match
examples/regex/search.py
import re text = 'The black cat climed' match = re.search(r'lac', text) if match: print("Matching") # Matching print(match.group(0)) # lac match = re.search(r'dog', text) 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.