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)
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.