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

Extract/Match string with "_" inbetween 2 words

I’m trying to extract any strings with a "_" in the middle.

For example, with name1_name2 _ nothing test1 _ test 2_ _3 I would like to extract name1_name2

Thank you for reading!

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
>>> s = 'name1_name2 _ nothing test1 _ test 2_ _3 name3_name4'
>>> re.findall('[a-zA-Z0-9]+_[a-zA-Z0-9]+', s)
['name1_name2', 'name3_name4']
>>> re.findall('\w+_\w+', s)
['name1_name2', 'name3_name4']
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