Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

using Regex to find the decimal numbers and exponential numbers

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading