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

使用正则表达式

import re
D = re.findall(r"\d+\.?\d*",line)
print(D)
-7.23246	10.8959	5.19534	0.0613837	-7.15631	10.815	-7.23983	10.9063	5.19994	0.00179692	0.0983757	0.0893044	0.406163	0.436051

['7.23246', '10.8959', '5.19534', '0.0613837', '7.15631', '10.815', '7.23983', '10.9063', '5.19994', '0.00179692', '0.0983757', '0.0893044', '0.406163', '0.436051']
import re
D = re.findall(r"-?\d+\.?\d*",line)
print(line)
print(D)
-7.23246	10.8959	5.19534	0.0613837	-7.15631	10.815	-7.23983	10.9063	5.19994	0.00179692	0.0983757	0.0893044	0.406163	0.436051

['-7.23246', '10.8959', '5.19534', '0.0613837', '-7.15631', '10.815', '-7.23983', '10.9063', '5.19994', '0.00179692', '0.0983757', '0.0893044', '0.406163', '0.436051']

正则表达式相关链接:

https://kb.cnblogs.com/page/96414/

生成正则表达式:
http://www.txt2re.com/

posted on 2017-12-18 09:54  lion_zheng  阅读(2388)  评论(0编辑  收藏  举报

导航