Python 查找可能后跟小数点的所有数字
我试图找到一个数字后跟小数点和另一个数字的组合。最后一个小数点可能缺失Python 查找可能后跟小数点的所有数字,python,regex,Python,Regex,我试图找到一个数字后跟小数点和另一个数字的组合。最后一个小数点可能缺失 E.g., 1.3.3 --> Here there are two combinations 1.3 and 3.3 1.3.3. --> Here there are two combinations 1.3 and 3.3 但是,当我运行以下代码时 st='1.2.3 The Mismatch of Accommodation and Disparity and the Depths of Focus
E.g.,
1.3.3 --> Here there are two combinations 1.3 and 3.3
1.3.3. --> Here there are two combinations 1.3 and 3.3
但是,当我运行以下代码时
st='1.2.3 The Mismatch of Accommodation and Disparity and the Depths of Focus and of Field'
import re
re.findall('\d\.\d+',st)
['1.2']
我做错了什么?因为不能两次匹配相同的字符,所以需要在先行断言中放置捕获组,以避免使用点右侧的数字:
re.findall(r'(?=(\d+\.\d+))\d+\.', st)
您可以匹配消费模式中的1+位数,并捕获正向前瞻中的小数部分,然后加入以下组:
import re
st='1.2.3 The Mismatch of Accommodation and Disparity and the Depths of Focus and of Field'
print(["{}{}".format(x,y) for x,y in re.findall(r'(\d+)(?=(\.\d+))',st)])
请参阅和
正则表达式详细信息:
-第1组:一个或多个数字(\d+)
-积极的前瞻性,需要存在:(?=(\。\d+)
-第2组:一个点,然后是1+个数字(\。\d+)
re.findall('\d\.\d*',st)
+
至少需要一个右手数字,在上一个示例中没有。此外,使用数字会导致下一个数字不匹配。