regex for Twitter username

Juanjo Conti picture Juanjo Conti · Feb 21, 2010 · Viewed 31.1k times · Source

Could you provide a regex that match Twitter usernames?

Extra bonus if a Python example is provided.

Answer

Angel.King.47 picture Angel.King.47 · Jun 15, 2011
(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9-_]+)

I've used this as it disregards emails.

Here is a sample tweet:

@Hello how are @you doing @my_friend, email @000 me @ [email protected] @shahmirj

Matches:

  • @Hello
  • @you
  • @my_friend
  • @shahmirj

It will also work for hashtags, I use the same expression with the @ changed to #.