简体   繁体   English

在 Python 中检测 re (regexp) object

[英]Detect re (regexp) object in Python

I wonder what is the proper pythonic backward- and forward-compatible method how check if an object is compiled re object.我想知道正确的 pythonic 向后和向前兼容方法是如何检查 object 是否re编译 object。

isinstance method cannot be easily used, while the resulting object claims to be _sre.SRE_Pattern object: isinstance方法不容易使用,而生成的 object 声称是_sre.SRE_Pattern object:

>>> import re
>>> rex = re.compile('')
>>> rex
<_sre.SRE_Pattern object at 0x7f63db414390>

but there is no such one:但没有这样的:

>>> import _sre
>>> _sre.SRE_Pattern
AttributeError: 'module' object has no attribute 'SRE_Pattern'

>>> import sre
__main__:1: DeprecationWarning: The sre module is deprecated, please import re.
>>> sre.SRE_Pattern
AttributeError: 'module' object has no attribute 'SRE_Pattern'

>>> re.SRE_Pattern
AttributeError: 'module' object has no attribute 'SRE_Pattern'

I don't want to use duck typing (ie checking for the availability of some specific methods), because this could collide with some other types.我不想使用鸭子类型(即检查某些特定方法的可用性),因为这可能会与其他一些类型发生冲突。

For now, I'm using:目前,我正在使用:

>>> RegexpType = type(re.compile(''))
>>> type(rex) == RegexpType
True

but there might be a better way..但可能有更好的方法..

re._pattern_type exists, and appears to do what you want: re._pattern_type存在,并且似乎可以执行您想要的操作:

>>> isinstance(re.compile(''), re._pattern_type)
True

But this is not a good idea - per Python convention, names starting with _ are not part of the public API of a module and not part of the backward compatibility guarantees.但这不是一个好主意 - 根据 Python 约定,以 _ 开头的名称不是模块的公共 API 的一部分,也不是向后兼容性保证的一部分。 So, using type(re.compile('')) is your best bet - though notice that this isn't guaranteed to work either, since the re module makes no mention of the object returned from re.compile() being of any particular class.所以,使用type(re.compile(''))是你最好的选择——尽管注意这也不能保证工作,因为 re 模块没有提到从 re.compile() 返回的 object 是任何特别是 class。

And indeed, even if this was guaranteed, the most Pythonic and back- and forward- compatible way would be to rely on the interface , rather than the type.事实上,即使保证了这一点,最 Pythonic 和向后和向前兼容的方式将是依赖接口,而不是类型。 In other words, embracing duck typing and EAFP, do something like this:换句话说,拥抱鸭子打字和 EAFP,做这样的事情:

try:
     rex.match(my_string)
except AttributeError:
     # rex is not an re
else:
     # rex is an re

Following some of the recommendations you can come up with:遵循您可以提出的一些建议:

import re

# global constant
RE_TYPE = re.compile('').__class__

def is_regex(a):
    return isinstance(a, RE_TYPE)

In similar question there wasn't any answer other than solution you use, so I think there is no better way.类似的问题中,除了您使用的解决方案之外没有任何答案,所以我认为没有更好的方法。

import re

print isinstance(<yourvar>, re.RE_Pattern)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM