How to extract usernames out of Tweets?
Tested:
/@([a-z0-9_]+)/i
In Ruby (irb):
>> "RT @user1: who are @thing and @user2?".scan(/@([a-z0-9_]+)/i)
=> [["user1"], ["thing"], ["user2"]]
In Python:
>>> import re
>>> re.findall("@([a-z0-9_]+)", "RT @user1: who are @thing and @user2?", re.I)
['user1', 'thing', 'user2']
In PHP:
<?PHP
$matches = array();
preg_match_all(
"/@([a-z0-9_]+)/i",
"RT @user1: who are @thing and @user2?",
$matches);
print_r($matches[1]);
?>
Array
(
[0] => user1
[1] => thing
[2] => user2
)
try an iterator (findall) with this regex:
(@[\w-]+)
bye
/(?<!\w)@(\w+)/
The above covers the following scenario, which other answers in this thread do not:
- An @ sign that is not supposed to be a username, e.g. "my email is [email protected]"
- Still allows a username that is at the beginning of a string, e.g. "@username lorem ipsum..."