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 values in name=value lines with regex

I’m really sorry for asking because there are some questions like this around. But can’t get the answer fixed to make problem.

This are the input lines (e.g. from a config file)

profile2.name=share2
profile8.name=share8
profile4.name=shareSSH
profile9.name=share9

I just want to extract the values behind the = sign with Python 3.9. regex.

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

I tried this on regex101.

^profile[0-9]\.name=(.*?)

But this gives me the variable name including the = sign as result; e.g. profile2.name=. But I want exactly the inverted opposite.

The expected results (what Pythons re.find_all() return) are

['share2', 'share8', 'shareSSH', 'share9']

>Solution :

Try pattern profile\d+\.name=(.*), look at Regex 101 example

import re
re.findall('profile\d+\.name=(.*)', txt)
# output 
['share2', 'share8', 'shareSSH', 'share9']

But this problem doesn’t necessarily need regex, split should work absolutely fine:

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