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

Python – How to get only the numbers from a string which has got commas and hyphens in between

Consider like I have a string :

stringA = "values-are-10-1,20-2,30-1,40-4,50-3"

I need to get only the strings : desired output :

for stringA: 10-1,20-2,30-1,40-4,50-3

Could you please help me in getting regex for this.

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 :

I suggest you use regex module:

import re
stringA = "values-are-10-1,20-2,30-1,40-4,50-3"
re.sub("[^0-9\-\,]", "", stringA).strip("-")

Output

10-1,20-2,30-1,40-4,50-3
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