如何使用regex获得带“.”的数字

2024-10-03 04:34:40 发布

您现在位置: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)

但我得到的错误是每个的最后一个数字。所以意思是ETC40.5会返回我5而不是40.5

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


Tags: inrenewfor错误数字regexhelloworld
1条回答
网友
1楼 · 发布于 2024-10-03 04:34:40

你得到的错误是由于你试图打开包装。请注意,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]

相关问题 更多 >