yurasharko
yurasharko

Reputation: 11

Regexp twitter handle Python

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

Answers (1)

Unmitigated
Unmitigated

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

Related Questions