匹配浮点数的 Python 正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12929308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:11:58  来源:igfitidea点击:

Python regular expression that matches floating point numbers

pythonregexfloating-point

提问by Johan R?de

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?

如何编写与浮点数的字符串表示相匹配的 Python 正则表达式?

The expression should match any string that is accepted by the floatconstructor as in float('3.5'). Thus the expression should match '0.'and '.0'but not '.'

该表达式应匹配float构造函数接受的任何字符串,如float('3.5'). 因此表达式应该匹配'0.''.0'但不匹配'.'

There is no need to match string representations of infinity and NaN.

不需要匹配无穷大和 NaN 的字符串表示。

采纳答案by Johan R?de

r'[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?'