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 can we extract a next numeric value to a string in python

There is a need that I have a taken a output in a string such as string is
"Initial: [818 v1] any other string or words here" and here I want to take 818 from this string. This 818 can change so can’t hard code it.

>Solution :

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


s = "Initial: [818 v1] any other string or words here"

import re
p = re.findall(r'\d+',s)
print(p)

OUTPUT

['818','1']

But if you only want the number which has more than 1 character Then

s = "Initial: [818 v1] any other string or words here"

import re
p = re.findall(r'\d+',s)
p = list(filter(lambda e:len(e)-1,p))
print(p)

OUTPUT:

['818']
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