与浮点数匹配的Python正则表达式 [英] Python regular expression that matches floating point numbers
本文介绍了与浮点数匹配的Python正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何编写一个匹配浮点数的字符串表达式的Python正则表达式?
字符串,被 float
构造函数接受,如 float('3.5')
>所接受。因此表达式应该匹配'0。'
和'。0'
但不是'。 '
无需匹配无穷大和NaN的字符串表示形式。
解决方案
r'[+ - ]?(\ d +(\.\d *)?| \.\d +)( [eE] [+ - ]?\d +)?'
Possible Duplicate:
How to detect a floating point number using a regular expression
How do I write a Python regular expression that matches string representations of floating point numbers?
The expression should match any string that is accepted by the float
constructor as in float('3.5')
. Thus the expression should match '0.'
and '.0'
but not '.'
There is no need to match string representations of infinity and NaN.
解决方案
r'[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?'
这篇关于与浮点数匹配的Python正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文