How can I create a regular expression for accepting numbers in Python? The numbers can be either integers, floats or of the format 3e+3 or 3e-3.
I want to match only the beginning of the string, and if a number in any of the above mentioned formats is present, return that number and the rest of the string.
Edit:
For example,
Input>> 290.07abcd Output>> [290.07, abcd]
Input>> abc123 Output>> None
Also, only the first occurrence is to be checked for.
For example,
Input>> -390-400abc
Output>>[-390, -400abc]
How can I do this using Python? I have tried the following, but it is not giving me the expected output:
import re
r = input()
x = re.search('^[+-]?\d*(\.\d+)?([+-][eE]\d+)?', r)
if x:
print("x present: ", x.group())
else:
print(None)
For example,
Input>> 100abc
Output>> x present: 100
Input>> abc100
Output>> x present:
Expected Output>> None
.123e2is a valid floating point expression (no leading 0 before the decimal point, and no explicit sign in the exponent).