编程语言
首页 > 编程语言> > 正则匹配——python用一个正则表达式从字符串中提取数字(包括整数、小数、正负数)

正则匹配——python用一个正则表达式从字符串中提取数字(包括整数、小数、正负数)

作者:互联网

import re

# 从字符串中提取数字
totalCount = '-100,abc2.4-123s,d-1ds-0.234as123.2s1.3bb.24'
count = re.findall('-?\d+.?\d+', totalCount)
print(count)

得到结果:[’-100’, ‘2.4’, ‘-123’, ‘-0.234’, ‘123.2’, ‘1.3’, ‘24’]

注意:无法匹配如.241.这种格式的数字,如果它们代表0.241.0,那么需要其他操作。

标签:24,count,totalCount,python,0.234,正负数,正则,re,100
来源: https://blog.csdn.net/weixin_35757704/article/details/120807129