RegExp:如何从Tweets(twitter.com)中提取用户名?

caw*_*caw 8 regex twitter

我有以下示例推文:

RT @ user1:谁是@thing和@ user2?

我只想拥有user1,thinguser2.

我可以用什么正则表达式来提取这三个名字?

PS:用户名必须只包含字母,数字和下划线.

Ste*_*rig 17

测试:

/@([a-z0-9_]+)/i
Run Code Online (Sandbox Code Playgroud)

在Ruby(irb)中:

>> "RT @user1: who are @thing and @user2?".scan(/@([a-z0-9_]+)/i)
=> [["user1"], ["thing"], ["user2"]]
Run Code Online (Sandbox Code Playgroud)

在Python中:

>>> import re
>>> re.findall("@([a-z0-9_]+)", "RT @user1: who are @thing and @user2?", re.I)
['user1', 'thing', 'user2']
Run Code Online (Sandbox Code Playgroud)

在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
)
Run Code Online (Sandbox Code Playgroud)