Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Regexes first match

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.

  • r
  • re
  • search|re
  • group|re