Edw*_*ang 10 python regex negative-lookahead lookbehind
我有以下场景:
1) car on the right shoulder
2) car on the left shoulder
3) car on the shoulder
Run Code Online (Sandbox Code Playgroud)
当左边没有时,我想匹配"肩膀".所以只有3)回归"肩膀"
re.compile(r'(?<!right|right\s*)shoulder')
sre_constants.error: look-behind requires fixed-width pattern
Run Code Online (Sandbox Code Playgroud)
好像我不能用\ s*和"|"
我该怎么解决这个问题.
提前致谢!
zx8*_*x81 21
regex 模块:可变宽度的lookbehind除了HamZa的答案之外,对于Python中任何复杂性的任何正则表达式,我建议使用regexMatthew Barnett的杰出模块.它支持无限的looknehind  - 这是为数不多的几个引擎之一,以及.NET和JGSoft.
这允许你做例如:
import regex
if regex.search("(?<!right |left )shoulder", "left shoulder"):
    print("It matches!")
else:
    print("Nah... No match.")
Run Code Online (Sandbox Code Playgroud)
\s+如果你愿意,你也可以使用.
输出:
It matches!
Run Code Online (Sandbox Code Playgroud)
        |   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           3761 次  |  
        
|   最近记录:  |