Python: Regex re.match
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, butre.match('me','somestring')
returnsNone
. 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