gpt4 book ai didi

python - 什么是 sre_constants.error : nothing to repeat

转载 作者:太空狗 更新时间:2023-10-29 21:15:10 24 4
gpt4 key购买 nike

我在使用看似简单的 Python 正则表达式时遇到了问题。

# e.g. If I wanted to find "mark has wonderful kittens, but they're mischievous.."
p = re.compile("*kittens*")

这将失败并出现错误:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.7/re.py", line 190, in compile
return _compile(pattern, flags)
File "/usr/lib64/python2.7/re.py", line 242, in _compile
raise error, v # invalid expression
sre_constants.error: nothing to repeat

我可能遗漏了一些非常简单的东西,正则表达式肯定不是我的强项!

最佳答案

您将正则表达式与 globs 混淆了.

你的意思是:

p = re.compile(".*kittens.*")

请注意,空星号在 RE 中的含义与在 glob 表达式中的含义不同。

关于python - 什么是 sre_constants.error : nothing to repeat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12390238/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com