Reputation: 355
I got the following scenarios:
1) car collisions effect 3 right lanes
2) 3 car collisions effect right lanes
I want to figure out number of lanes instead of number of collision. To be specific I want extract number and "right lanes" with less than two \bwords\b in between.
\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]?)
Upvotes: 3
Views: 56
Reputation: 97968
Using 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)
Gives:
['3']
[]
Upvotes: 3