有没有办法在python中从右到左解析正则表达式?
我有几个巨大的正则表达式需要大约一秒钟来运行我的输入,总运行时间为几分钟.所以我试着测试我的正则表达式的性能,并且regexhero可以选择从右到左解析正则表达式,这导致执行速度提高了大约一百万倍,因为更快的失败.
是的,方法是反转字符串(并根据新字符串写入模式):
串:
'John likes to eat mushrooms'[::-1]
Run Code Online (Sandbox Code Playgroud)
模式(像约翰?):
r'^(.+) sekil nohJ$'
Run Code Online (Sandbox Code Playgroud)
您还可以将re模块更改为提供反向搜索标志的regex模块(?r):
>>> import regex
>>> regex.findall(r'(?r)\w+(?=\W+\w*e)', 'John likes to eat mushrooms')
['to', 'John']
Run Code Online (Sandbox Code Playgroud)
(注意此功能,如.net RightToLeft选项,结果可能违反直觉.)
| 归档时间: |
|
| 查看次数: |
1960 次 |
| 最近记录: |