为了确保整个字符串匹配,您需要使用\Z
字符串结尾锚:
def fullmatch(regex, string, flags=0):
"""Emulate python-3.4 re.fullmatch()."""
return re.match("(?:" + regex + r")\Z", string, flags=flags)
该\A
锚是没有必要的,因为re.match()
已经锚定匹配字符串的开始。
将Python 3.4的正则表达式“ fullmatch()”移植到Python 2
为了确保整个字符串匹配,您需要使用\Z
字符串结尾锚:
def fullmatch(regex, string, flags=0):
"""Emulate python-3.4 re.fullmatch()."""
return re.match("(?:" + regex + r")\Z", string, flags=flags)
该\A
锚是没有必要的,因为re.match()
已经锚定匹配字符串的开始。