Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python正则词重复时间_Python_Regex - Fatal编程技术网

python正则词重复时间

python正则词重复时间,python,regex,Python,Regex,我得到了以下场景: 1) car collisions effect 3 right lanes 2) 3 car collisions effect right lanes 我想算出车道的数量,而不是碰撞的数量。具体来说,我希望提取数字和“右车道”,中间少于两个\b单词\b \b(\d)<I want to limit 2 words here>\s*(lane[s]?) OR \b(\d)<I want to limit 10 characters here>\s*

我得到了以下场景:

1) car collisions effect 3 right lanes
2) 3 car collisions effect right lanes
我想算出车道的数量,而不是碰撞的数量。具体来说,我希望提取数字和“右车道”,中间少于两个\b单词\b

\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]?)
\b(\d)\s*(车道?)
或
\b(\d)\s*(车道[s]?)
使用前瞻:

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) 
给出:


你应该从你的例子中得到什么结果?这解决了我的问题。谢谢。我有类似的解决方案,但我使用了{2}而不是{,2}。Python重新文档使用了{m}而不是{,m},但这是错误的
['3']
[]