import re
re.match("\$?(-?(\d+[,.])*\d+)", "$-12,000.01") # match
re.match("\$?(-?(\d+[,.])*\d+)", "$-12,000.01").group(1) # extract matched value
>>> '-12,000.01'
re.sub('[,$]', '', '$-12,000.01') # remove comma and dollar sign
>>> '-12000.01'
float(re.sub('[,$]', '', '$-12,000.01')) # convert to float if the result doesn't contain any special character such as comma
>>> -12000.01
正则表达式
示例:
\$?(-?(\d+[,.])*\d+)
如果数据集中有任何事例,请向正则表达式添加更多事例
可能存在许多无效的边缘案例,例如13.000000
此正则表达式将修复它:
\$?(-?\d*(\d+,)*\.?\d+)
因此,根据需要添加尽可能多的案例
相关问题 更多 >
编程相关推荐