re.match in Python

Looks for a match at the beginning of the line.

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

-> object Match / MatchObject, or None

pattern - Pattern to match

string - String to check for pattern matching .

flags = 0 - Flags to control the interpretation of the regular expression. 

Returns a match object if zero or more characters at the beginning of the line match the pattern, otherwise returns None .

  import re 

re.match (` cat`, `cat-crap`) # & lt; _sre.SRE_Match at 0x7f29f544a440 & gt;
re.match (`cat`,` dog cat`, re.MULTILINE) # None
re.match (`cat`,` not cat`) # None


Even if MULTILINE mode is enabled the function searches exclusively at the beginning of the line. If you want to search for a match anywhere in a string, use re.search () .

Get Solution for free from DataCamp guru