I want to write a regular expression to read these numbers:
numbers = '''344.0392 343.4564 343.7741E-3
343.9302 343.3884 343.7685 0.0000 341.0843
342.2441 342.5899 343.0728 343.4850 342.8882E-6'''
I used \d+\.\d+ for the decimal numbers. but I have the problem that it reads the decimal number before E beside others.
I used \d+\.\d+[Ee][+-]\d+ to read the 343.7741E-3 and 342.8882E-6 which works well.
But I don’t know why for my case when I use | between the first and second expressions it doesn’t work properly.
>Solution :
I modified your regex to this: \d+\.\d+(?:e[+-]\d|E[-+]\d)?
This uses a non capture group to match the exponent part.
Test: https://regex101.com/r/8j8VJn/1
\d+\.\d+(?:e[+-]\d|E[-+]\d)?
\d+. matches one or more digits before the decimal number. If you want to allow numbers like .123, .45 etc then replace the + with *
\d+ matches one or more digits after the decimal
(?: non capture group
e[+-]\d|E[-+]\d matches exponent part, can begin with 'e' or 'E' and followed by '+' or '-'. eg: 'e-/+334' or 'E-/+334'