Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python regular expression that matches floating point numbers [duplicate]

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.

like image 834
Johan Råde Avatar asked Oct 17 '12 07:10

Johan Råde


People also ask

What is the expression to represent floating point number?

[0-9]+|[0-9]+). This regular expression matches an optional sign, that is either followed by zero or more digits followed by a dot and one or more digits (a floating point number with optional integer part), or that is followed by one or more digits (an integer).

How do you extract a float number from a string in Python?

To extract a floating number from a string with Python, we call the re. findall method. import re d = re. findall("\d+\.

What is Backreference in regular expression Python?

Introduction to the Python regex backreferences The backreferences allow you to reference capturing groups within a regular expression. In this syntax, N can be 1, 2, 3, etc. that represents the corresponding capturing group. Note that the \g<0> refer to the entire match, which has the same value as the match.

How do you repeat in regex?

A repeat is an expression that is repeated an arbitrary number of times. An expression followed by '*' can be repeated any number of times, including zero. An expression followed by '+' can be repeated any number of times, but at least once.


1 Answers

r'[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?'
like image 160
Johan Råde Avatar answered Oct 26 '22 10:10

Johan Råde