If I have a string containing tags formatted like <@tag> such as:
text = "In this test I tag <@bill>, <@Jennifer>, and lastly <@bob>."
how might I get a python list of the tags extracted for purposes of iterating through the tags.
['bill','Jennifer','bob']
While this is going to be specifically applied to a Slack Chatops Bot I am working on, I left it generic as it might be useful for other things. And I failing at how to for a decent google search to solve it, and the suggested questions in Stack Exchange did not touch on this already..
Thanks! Nick
You can use regular expressions:
import re
text = "In this test I tag <@bill>, <@Jennifer>, and lastly <@bob>."
print(re.findall('<@(.+?)>', text)) # ['bill', 'Jennifer', 'bob']
Basic explanation:
()
denotes a capturing group, i.e. 'extract this part for me'.
means 'any character'.+
means 'any character one or more times'.+?
means 'any character one or more times, but as few as possible', otherwise it would include the >
and many more characters after that:print(re.findall('<@(.+)>', text)) # ['bill>, <@Jennifer>, and lastly <@bob']