I have the following example tweet:
RT @user1: who are @thing and @user2?
I only want to have user1, thing and user2.
What regular expression can I use to extract those three names?
PS: A username must only contain letters, numbers and underscores.
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
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With