我遇到了一个看似简单的Python正则表达式的问题.
# e.g. If I wanted to find "mark has wonderful kittens, but they're mischievous.."
p = re.compile("*kittens*")
Run Code Online (Sandbox Code Playgroud)
这将失败并显示错误:
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
Run Code Online (Sandbox Code Playgroud)
我可能错过了一些非常简单的东西,正则表达式肯定不是我的优势!
unw*_*ind 21
你把正则表达式与globs混淆了.
你的意思是:
p = re.compile(".*kittens.*")
Run Code Online (Sandbox Code Playgroud)
请注意,裸星号在RE中的含义与在glob表达式中的含义相同.
| 归档时间: |
|
| 查看次数: |
18407 次 |
| 最近记录: |