我有以下示例推文:
RT @ user1:谁是@thing和@ user2?
我只想拥有user1,thing和user2.
我可以用什么正则表达式来提取这三个名字?
PS:用户名必须只包含字母,数字和下划线.
测试:
/@([a-z0-9_]+)/i
在Ruby(irb)中:
>> "RT @user1: who are @thing and @user2?".scan(/@([a-z0-9_]+)/i) => [["user1"], ["thing"], ["user2"]]
在Python中:
>>> import re >>> re.findall("@([a-z0-9_]+)", "RT @user1: who are @thing and @user2?", re.I) ['user1', 'thing', 'user2']
在PHP中:
Array ( [0] => user1 [1] => thing [2] => user2 )