Could you provide a regex that match Twitter usernames?
Extra bonus if a Python example is provided.
Could you provide a regex that match Twitter usernames?
Extra bonus if a Python example is provided.
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)
The only characters accepted in the form are A-Z, 0-9, and underscore. Usernames are not case-sensitive, though, so you could use r'@(?i)[a-z0-9_]+'
to match everything correctly and also discern between users.
Twitter recently released to open source both java and ruby (gem) implementations of the code they use for finding user names, hash tags, lists and urls.
It is very regular expression oriented.
Here is a PHP function that links urls and also mailto and twitter usernames and arguments tags.