如何从推文中提取用户名? [英] How to extract usernames out of Tweets?

查看:44
本文介绍了如何从推文中提取用户名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下示例推文:

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

我只想拥有 user1thinguser2.

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

PS:用户名只能包含字母、数字和下划线.

解决方案

已测试:

/@([a-z0-9_]+)/i

<小时>

在 Ruby (irb) 中:

<代码>>>RT @user1:@thing 和@user2 是谁?".scan(/@([a-z0-9_]+)/i)=>[["user1"], ["thing"], ["user2"]]

在 Python 中:

<预><代码>>>>进口重新>>>re.findall("@([a-z0-9_]+)", "RT @user1:@thing 和@user2 是谁?", re.I)['用户1','东西','用户2']

在 PHP 中:

大批([0] =>用户 1[1] =>事物[2] =>用户 2)

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
)

这篇关于如何从推文中提取用户名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆