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

How to properly convert regex string to float

I have a string that I am attempting to extract, for instance, the string below, I want to extract only 3.10 but as a float so that if the number is under 3.9 I can add logic.

Here is what I have thus far

import re
x='3.10.8.10_IDNUM19191_SUITE'
m=re.search(r"([0-9]+(\.[0-9]+)+)", x)
m= (m.group(1))
print(m)

if m>='3.9':
  print("Number is 3.9.0 or higher")
else:
  print("Number is under 3.9.0")

I am unable to use float because it cannot convert ‘3.10.8.10’ to a float. The regex I have pulls out 3.10.8.10 instead of 3.10

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

>Solution :

import re
x='3.10.8.10_IDNUM19191_SUITE'
m=re.search(r"([0-9]+(\.[0-9]+))", x)
m= (m.group(1))
print(m)

if float(m)>=3.9:
  print("Number is 3.9.0 or higher")
else:
  print("Number is under 3.9.0")
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