python 从字符串中提取数字,使用正则表达式

python 从字符串中提取数字,负数、小数、浮点数

使用正则表达式:

import re
text = "line channel [7], device_id[98],  sf[7], rssi [-73.558510], snr [-7.000000], seq [1210],skip_seq 0 "
#par='-?\d+\.*\d*'
par='-?\d+\.*\d*'#? 前面- 一次或者多次;\d+ 匹配数字 +一个或者多个 \.小数点*0次或者多次
p1 = re.compile(par).findall(text)
print('result',p1)

输出结果:

result ['7', '98', '7', '-73.558510', '-7.000000', '1210', '0']

猜你喜欢

转载自blog.csdn.net/WANGYONGZIXUE/article/details/110879324
今日推荐