python - 是否可以在 python 中对已编译的正则表达式进行类型提示?

标签 python regex type-hinting

我想对预编译和存储的正则表达式列表使用自动完成功能,但我似乎无法导入 _sre.SRE_Pattern 类,而且我无法以编程方式从 type( ) 格式的注释 # type: classname 或将其用于返回 -> classname 样式提示

有没有办法从 _sre.c 中显式导入一个类?

最佳答案

你应该使用 typing.Pattern and typing.Match专门添加到 typing 模块以适应此用例。

例子:

from typing import Pattern, Match
import re

my_pattern = re.compile("[abc]*")  # type: Pattern[str]
my_match = re.match(my_pattern, "abbcab")  # type: Match[str]
print(my_match)

关于python - 是否可以在 python 中对已编译的正则表达式进行类型提示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38556579/

相关文章:

python - 是否可以创建受正则表达式约束的类型提示?

Python/Django - Vcard 照片

python - 基准测试 : does python have a faster way of walking a network folder?

python - cx_Oracle.NotSupportedError : Python value of type NAType not supported

python - 如何使用类型提示为参数指定多种类型?

生成器表达式的 Python PEP 484 类型注释

python - 在 Bokeh 中,奇怪的日期轴问题

用于将字符串转换为元组的正则表达式

java - 正则表达式替换为增量

java - 正则表达式清除 csv 中令人困惑的字符