Regex for Twitter username

Could you provide a regex that matches Twitter usernames?

An added bonus if a Python example is provided.

+38
python regex twitter
Feb 21 '10 at 3:19
source share
8 answers
(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9]+) 

I used this as it ignores emails

Here is an example tweet

 @Hello how are @you doing, email @000 me @ whats.up@example.com @shahmirj 

Selection:

 @Hello @you @shahmirj 

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

I have a blog entry in which I constantly update my @ http://shahmirj.com/blog/extracting-twitter-usertags-using-regex , do not forget to compare it simply, I found an error: D

That's right, I just got there: D

+64
. Jun 15 '11 at 1:01
source share

If you talk about the @username they use on twitter, you can use this:

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

To make each instance an HTML link, you can 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) 
+18
Feb 21 '10 at 3:25
source share

I use regex and tested in several contexts:

 /(^|[^@\w])@(\w{1,15})\b/ 

This is the cleanest way I've found to check and replace Twitter username in lines.

 #!/usr/bin/python import re text = "@RayFranco is answering to @jjconti, this is a real '@username83' but this is an@email.com, and this is a @probablyfaketwitterusername"; ftext = re.sub( r'(^|[^@\w])@(\w{1,15})\b', '\\1<a href="http://twitter.com/\\2">\\2</a>', text ) print ftext; 

This will bring me back as expected:

 <a href="http://twitter.com/RayFranco">RayFranco</a> is answering to <a href="http://twitter.com/jjconti">jjconti</a>, this is a real '<a href="http://twitter.com/username83">username83</a>' but this is an@email.com, and this is a @probablyfaketwitterusername 

Based on Twitter Specifics :

Your username cannot be longer than 15 characters. Your real name may be longer (20 characters), but usernames are kept shorter for convenience. The username can contain only alphanumeric characters (letters AZ, numbers 0-9), with the exception of underscores, as indicated above. Make sure your desired username does not contain characters, dashes, or spaces.

+15
Nov 15 '12 at 11:48
source share

Twitter has recently released open source code in a variety of languages, including Java, Ruby ( gem ), and Javascript implementations of the code that they use to search for usernames, hash tags, lists, and URLs.

This is a very regular expression, oriented.

+11
Feb 24 '10 at 22:55
source share

The only characters accepted in the form are AZ, 0-9 and the underscore. Usernames are not case sensitive, so you can use r'@(?i)[a-z0-9_]+' to combine everything correctly, as well as distinguish between users.

+2
Feb 21 '10 at 3:56
source share

In short, /@([\w]+)/ works fine.

+1
Feb 24 '10 at 23:19
source share

This is the method I used in the project, which takes the text attribute of the tweet object and returns the text with both hashtags and user_mentions associated with the corresponding twitter pages, in accordance with the latest recommendations for displaying twitter.

 def link_tweet(tweet): """ This method takes the text attribute from a tweet object and returns it with user_mentions and hashtags linked """ tweet = re.sub(r'(\A|\s)@(\w+)', r'\1@<a href="http://www.twitter.com/\2">\2</a>', str(tweet)) return re.sub(r'(\A|\s)#(\w+)', r'\1#<a href="http://search.twitter.com/search?q=%23\2">\2</a>', str(tweet)) 

Once you call this method, you can pass the parameter my_tweet [x] .text. Hope this will be helpful.

+1
Feb 20 '13 at 21:00
source share

This regex seems to resolve Twitter usernames:

 ^@[A-Za-z0-9_]{1,15}$ 

A maximum of 15 characters allows underline immediately after @ (which Twitter does) and allows all underscores (which, after a quick search, I found that Twitter obviously also does). Excludes email addresses.

+1
Mar 14 '14 at 19:45
source share



All Articles