tags:

views:

111

answers:

4

Hi,

I'm looking to match Twitter syntax with a regex.

How can I match anything that is "@______" that is, begins with an @ symbol, and is followed by no spaces, just letters and numbers until the end of the word? (To tweeters, I want to match someone's name in a reply)

+4  A: 

@\w+

That simple?

Matt Kellogg
Does w include numbers?
DOK
You need a \ in front of your w - and you'd probably want to use +, not * (since an @ without a name isn't valid).
Amber
I had a slash in there, must've nuked it when I changed the * to a +
Matt Kellogg
@DOK: POSIX standard regex says that \w should match alphanumerics and underscores.
Amber
As Dav mentioned, \w is generally equivalent to [A-Za-z0-9_] - but worth noting that some regex engines also include accented characters (i.e. á/ê/ö and so on) in their 'word character' definition. Good idea to quickly test whatever regex flavour is being used, if this might cause an issue.
Peter Boughton
A: 
@[\d\w]+

\d for a digit character
\w for a word character
[] to denote a character class
+ to represent more than one instances of the character class

Note that these specifiers for word and digit characters are language dependent. Check the language specification to be sure.

Willi Ballenthin
\w actually includes digits, so @\w+ will suffice.
Amber
\w is alphanumeric
Matt Kellogg
The other answer with just "\w" is the best one.
richardtallent
+5  A: 

Go for

/@(\w+)/

to get the matching name extracted as well.

richsage
+1  A: 

It should be noted that Twitter no longer allows usernames longer than 15 characters, so you can also match with:

@\w{1,15}

There are still apparently a few people with usernames longer than 15 characters, but testing on 15 would be better if you want to exclude likely false positives.

There are apparently no rules regarding whether underscores can be used the the beginning or end of usernames, multiple underscores, etc., and there are accounts with single-letter names, as well as someone with the username "_".

richardtallent