如何用“。”获取数字。使用正则表达式

时间:2019-07-26 18:24:37

标签: python regex

所以我一直在尝试使用正则表达式(仍在学习中),并试图获取此组合的最后一个值

new avCombination('919712-041','HelloWorld','40.5');
new avCombination('919712-041','HelloWorld','41');
new avCombination('919712-041','HelloWorld','42');
new avCombination('919712-041','HelloWorld','42.5');
new avCombination('919712-041','HelloWorld','43');
new avCombination('919712-041','HelloWorld','44');
new avCombination('919712-041','HelloWorld','44.5');
new avCombination('919712-041','HelloWorld','45');
new avCombination('919712-041','HelloWorld','45.5');

我一直在尝试使用python,但是我被困在

*Updated:* 
for values in re.findall('(\d+)(?=\'\);)', listOfCombinations)):
    print(values)

但是我得到的错误是每个的最后一个数字。这样的意思是etc 40.5只会返回5而不是40.5

,我相信我做错了正则表达式!我该怎么解决?

1 个答案:

答案 0 :(得分:1)

您遇到的错误是由于尝试打开包装而引起的。请注意,re.findall()返回一个列表,因此您可能需要执行以下操作:

for i in re.findall(.....):
    print(i)

假设您要解析的JS函数中的最后一个参数始终为float,则可以执行以下操作(其中s是包含所有JS函数的字符串):

list(map(float, re.findall(r'([\d\.]+)(?=\'\);)', s)))

收益:

[40.5, 41.0, 42.0, 42.5, 43.0, 44.0, 44.5, 45.0, 45.5]