Python正则表达式搜索带逗号的浮点数值

时间:2018-08-24 06:18:21

标签: python regex python-3.x

我有类似的字符串:

a = "opening Balance 25,07,708.33 |Fixed Assets (Furniture)"
b = "|Add: income during the Yr, 5,95,719.92 |0/Balance 55,221.00"
c = "|Les:- Dep@10% 5,522.00 49,699.00"

我只想从列表中的字符串中获取浮点数值:

a_l = ["25,07,708.33"]
b_l = ["5,95,719.92" , "55,221.00"]
c_l = ["5,522.00" , "49,699.00"]

我正在使用python中的正则表达式尝试这段代码:

import re
a_l = a[re.search(r'((\d+\,+)+\d+\.\d{2})', a).span()[0]:re.search(r'((\d+\,+)+\d+\.\d{2})', a).span()[1]]
# same for b_l, c_l

我得到的输出如下:

a_l = '25,07,708.33'
b_l = '5,95,719.92'
c_l = '5,522.00'

它只是检测到这种首次出现的情况,然后忽略其他情况。在浮点数值中搜索而不替换逗号的正确模式是什么?

1 个答案:

答案 0 :(得分:1)

您可以轻松完成

b = "|Add: income during the Yr, 5,95,719.92 |0/Balance 55,221.00"
import re
a_l = re.findall(r'((?:\d+,+)+\d+\.\d{2})', b)
print a_l

输出: ['5,95,719.92', '55,221.00']