我有多个字符串,如:
a = 'avg yearly income 25,07,708.33 '
b = 'current balance 1,25,000.00 in cash\n'
c = 'target savings 50,00,000.00 within next five years 1,000,000.00 '
我试图将它们拆分为文本字符串和数字字符串的块,示例输出如下:
aa = [('avg yearly income', '25,07,708.33')]
bb = [('current balance', '1,25,000.00', 'in cash')]
cc = [('target savings', '50,00,000.00', 'within next five years', '1,000,000.00')]
我正在使用以下代码:
import re
b = b.replace("\n","")
aa = re.findall(r'(.*)\s+(\d+(?:,\d+)*(?:\.\d){1,2})', a)
bb = re.findall(r'(.*)\s+(\d+(?:,\d+)*(?:\.\d){1,2})(.*)\s+', b)
cc = re.findall(r'(.*)\s+(\d+(?:,\d+)*(?:\.\d){1,2})(.*)\s+(\d+(?:,\d+)*(?:\.\d{1,2})?)', c)
我得到以下输出:
aa = [('avg yearly income', '25,07,708.3')]
bb = [('current balance', '1,25,000.0', '0 in')]
cc = [('target savings', '50,00,000.0', '0 within next five years', '1,000,000.00')]
正则表达式的模式有什么问题?
萧十郎
繁花不似锦
杨魅力
相关分类