regex for Twitter username


Question

Could you provide a regex that match Twitter usernames?

Extra bonus if a Python example is provided.

1
42
2/21/2010 3:25:43 AM

Accepted Answer

(?<=^|(?<=[^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 @ whats.up@example.com @shahmirj

Matches:

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

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

71
4/15/2018 5:54:17 AM

If you're talking about the @username thing they use on twitter, then you can use this:

import re
twitter_username_re = re.compile(r'@([A-Za-z0-9_]+)')

To make every instance an HTML link, you could do something like this:

my_html_str = twitter_username_re.sub(lambda m: '<a href="http://twitter.com/%s">%s</a>' % (m.group(1), m.group(0)), my_tweet)

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon