如何编写匹配浮点数的Python正则表达式?
以下代码使用Python正则表达式匹配浮点数
更多Python相关文章,请阅读:Python 教程
示例
import re
s = '234.6789'
match = re.match(r'[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?',s)
print match.group()
s2 = '0.45'
match = re.match(r'[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?',s2)
print match.group()
输出
这将给出以下输出结果:
234.6789
0.45
极客教程