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 re.split() to split by comma and specific

I am a beginner in python and I would like to use re.split() to find a specific value from a string.

For example I have this input string :

input = "type: type1, blablabla, blabla, role: user (user1) and admin (admin1), blablabla, use: use1"

I would like to extract this output : user (user1) and admin (admin1)

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 have tried some regex like role:\s(\w+\s), :

output = re.split('role:\s(\w+\s),', input)

My thinking :

#role:\s : match 'role:' and space
#(\w+\s) : match one or more occurence of word characters and space
#,       : end with comma 

Which parts are missing in the second part of the regex to match any words, separate by space, that end with a comma ?

A solution will be helpful but above all I would like to understand how it works.

Thanks for your help

>Solution :

In general, I only use regular expression when I absolutely must. I like those solutions that are easy to understand. For that, I am going to suggest going with longer code:

found = None
for element in input.split(","):
    if "role:" in element:
        found = element.replace("role:", "").strip()
        break
# found is 'user (user1) and admin (admin1)'

The way this code work is when you split by the comma, you get:

['type: type1',
 ' blablabla',
 ' blabla',
 ' role: user (user1) and admin (admin1)',
 ' blablabla',
 ' use: use1']

Then the if statement test for the presence of "role:" in each element and parse out the needed output.

Update

If you must use regular expression:

matched = re.search(r"role:\s([^,]+)", input)
print(matched.group(1))  # 'user (user1) and admin (admin1)'

As you can see, this solution is shorter, but more cryptic.

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