我有以下情况:
1) car collisions effect 3 right lanes
2) 3 car collisions effect right lanes
我想弄清楚车道数而不是碰撞次数。具体而言,我想要提取数字和“右边的通道”,其间的间距少于两个\ bwords \。
\b(\d)<I want to limit 2 words here>\s*(lane[s]?)
OR
\b(\d)<I want to limit 10 characters here>\s*(lane[s]?)
答案 0 :(得分:3)
使用lookahead:
import re
s1 = "1) car collisions effect 3 right lanes"
s2 = "2) 3 car collisions effect right lanes"
print re.findall("(\d+)(?=(?:\s+\w+){,2}\s+right lanes)", s1)
print re.findall("(\d+)(?=(?:\s+\w+){,2}\s+right lanes)", s2)
给出:
['3']
[]