I am trying to find out if a number is represented using scientific notation. I already tested my regex pattern with valid/invalid test cases using online regex tool. However, when I use use my regex pattern in python, it doesn't match some valid test cases.
Here is the regex pattern:
^-?([1-9]{1}|[1-9]?\.[0-9]+)[eE][+\-]?0?[1-9]+0*$
valid test cases:
-9.99E+9
9E-1
1e-12
1e12
1e-06
1e-066
1e6000
4.2e06
4.2e-06
4.2e60
.1e12
3.2e23
-4.70e+9
-.2E-4
4e6666
4e6660
4e-6666
invalid test cases:
37.e88
1.2e001
10e1
0.0e12
-0.9e2
-9e-0
0e12
9.3e0.2
0e000
1e00009
1e00090
1e000
1
1.000
e112
45e12
0.1e12
but if I try in python:
pat = re.compile('^-?([1-9]{1}|[1-9]?\.[0-9]+)[eE][+\-]?0?[1-9]+0*$')
match = re.search(pat, str(4.2e6))
it returns None. this is a valid test case. Also, 4.2e06, 4.2e666, 4.2e-66 are all valid test cases but it returns None. why does it work for online regex tool but not python regex engine?
I looked at some stackoverflow posts and tried answers given there such as: pat = re.compile(r'^[+-]?(?:0|[1-9]\d*)(?:.\d*)?(?:[eE][+-]?\d+)$')
It doesn't work for 4.2e06, 4.2e666, 4.2e-66.
Just fix your match search by
match = re.search(pat, '4.2e6')