Python: Regex re.match

By Xah Lee. Date: . Last updated: .
re.match(regex, text)
Similar to re.search except that the match must start at the beginning of string.

For example, re.search('me','somestring') matches, but re.match('me','somestring') returns None.

re.match(regex, text, flags=flags)
Use Flags .
import re

xx = re.match('so','somestring')

if xx == None:
    print
    print("no match")
else:
    print("yes match")

Note: re.match() is not exactly equivalent to re.search() with ^.

import re

print(re.search(r"^B", "A\nB",re.M))
# <re.Match object; span=(2, 3), match='B'>

print(re.match(r"B", "A\nB",re.M))
# None

Python, Regex Functions

Python, Regular Expression