python中已编译的正则表达式的类型是什么?
我特别要评估
isinstance(re.compile(''), ???)
确实是出于自省的目的。
我的一个解决方案是,具有一些全局常量REGEX_TYPE = type(re.compile(''))
,但是它看起来并不优雅。
编辑:我想这样做的原因是因为我有字符串列表和已编译的正则表达式对象。我想根据列表“匹配”字符串
- 对于列表中的每个字符串,请尝试检查字符串是否相等。
- 对于列表中的每个正则表达式,请尝试检查字符串是否与给定的模式匹配。
我想到的代码是:
for allowed in alloweds:
if isinstance(allowed, basestring) and allowed == input:
ignored = False
break
elif isinstance(allowed, REGEX_TYPE) and allowed.match(input):
ignored = False
break