我需要检查一个变量是否是正则表达式匹配对象。
print(type(m))返回类似这样的内容:<_sre.SRE_Match object at 0x000000000345BE68>
但是,当我导入_sre并尝试执行type(m) is SRE_Match时,异常NameError: name 'SRE_Match' is not defined被抛出。
发布于 2012-09-20 14:20:33
你可以做到
SRE_MATCH_TYPE = type(re.match("", ""))在程序开始时,然后
type(m) is SRE_MATCH_TYPE每次你想要做比较的时候。
发布于 2012-09-20 15:37:00
堆积如山,因为有一大堆方法可以解决这个问题:
def is_match_obj(m):
t = type(m)
return (t.__module__, t.__name__) == ('_sre', 'SRE_Match')发布于 2019-09-04 18:19:23
from typing import Match
isinstance(m, Match)https://stackoverflow.com/questions/12506926
复制相似问题