python中编译的正则表达式对象的类型 [英] Type of compiled regex object in python

查看:41
本文介绍了python中编译的正则表达式对象的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

python中编译出来的正则表达式是什么类型的?

特别想评价

isinstance(re.compile(''), ???)

是真实的,出于内省的目的.

我的一个解决方案是,使用一些全局常量 REGEX_TYPE = type(re.compile('')),但它看起来不太优雅.

我想这样做的原因是因为我有字符串列表和编译的正则表达式对象.我想根据列表匹配"一个字符串,通过

  • 对于列表中的每个字符串,尝试检查字符串是否相等.
  • 对于列表中的每个正则表达式,尝试检查字符串是否与给定的模式匹配.

我想出的代码是:

 for allowed in alloweds:如果 isinstance(allowed, basestring) 和 allowed == 输入:忽略 = 错误休息elif isinstance(allowed, REGEX_TYPE) 和 allowed.match(input):忽略 = 错误休息

解决方案

当某事物的类型没有很好地指定时,使用 type 内置函数在运行时发现答案并没有错:

<预><代码>>>>进口重新>>>retype = type(re.compile('hello, world'))>>>isinstance(re.compile('goodbye'), retype)真的>>>isinstance(12, 重新输入)错误的>>>

在运行时发现类型可以保护您免于访问私有属性以及将来对返回类型的更改.在这里使用 type 并没有什么不雅,尽管想要知道类型可能有点不雅.

也就是说,随着时间的推移,这个问题的上下文已经发生了变化.使用当代版本的 Python,re.compile 的返回类型 现在是 re.Pattern.

关于如果某物的类型没有明确指定该怎么办的一般问题仍然有效,但在这种特殊情况下,re.compile(...) 的类型 现在已经明确.

What is the type of the compiled regular expression in python?

In particular, I want to evaluate

isinstance(re.compile(''), ???)

to be true, for introspection purposes.

One solution I had was, have some global constant REGEX_TYPE = type(re.compile('')), but it doesn't seem very elegant.

EDIT: The reason I want to do this is because I have list of strings and compiled regex objects. I want to "match" a string against list, by

  • for each string in the list, try to check for string equality.
  • for each regex in the list, try to check whether the string matches the given pattern.

and the code that I came up with was:

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

解决方案

When the type of something isn't well specified, there's nothing wrong with using the type builtin to discover the answer at runtime:

>>> import re
>>> retype = type(re.compile('hello, world'))
>>> isinstance(re.compile('goodbye'), retype)
True
>>> isinstance(12, retype)
False
>>> 

Discovering the type at runtime protects you from having to access private attributes and against future changes to the return type. There's nothing inelegant about using type here, though there may be something inelegant about wanting to know the type at all.

That said, with the passage of time, the context of this question has shifted. With contemporary versions of Python, the return type of re.compile is now re.Pattern.

The general question about what to do if the type of something is not well-specified is still valid but in this particular case, the type of re.compile(...) is now well-specified.

这篇关于python中编译的正则表达式对象的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆