Reputation: 11
I need to get twitter handle from different type of data
https://twitter.com/elonmusk
https://twitter.com/elonmusk/status/43940840234234
https://twitter.com/elonmusk?t=w5i1O32q6dM7usSQEaTGvA&s=09
https://twitter.com/elonmusk
@elonmusk
all of this should return elonmusk
and also I can have more then 1 twitter handle in one message, so it should return all handles as list
https://twitter.com/elonmusk https://twitter.com/elonmusk/status/43940840234234
this should return elonmusk,elonmusk
Upvotes: 1
Views: 44
Reputation: 89412
import re
s = "some text..."
print(re.findall(r'(?:twitter\.com/|@)(\w+)', s))
This matches a sequence of word characters following "twitter.com/" or "@".
Upvotes: 2