1

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

yurasharko
  • 11
  • 1

1 Answers1

2
import re
s = "some text..."
print(re.findall(r'(?:twitter\.com/|@)(\w+)', s))

This matches a sequence of word characters following "twitter.com/" or "@".

Unmitigated
  • 76,500
  • 11
  • 62
  • 80