使用Powershell提取所有Twitter用户名

时间:2013-10-18 16:00:57

标签: regex parsing powershell twitter

我想解析一个字符串并返回一个包含该推文中提到的人的数组(列表)。 Twitter用户名都以@开头。

如何使用Powershell在字符串中提取所有Twitter用户名?

$text = @"
    text text text 
    @twitter text text text @facebook 
    text text text @linkedin text text text 
        Hello <a href="http://twitter.com/twitter">@twitter</a> and <a href="http://twitter.com/facebook">@facebook</a> @kiquenet
"@

我会得到:

@twitter 
@facebook 
@linkedin 
@kiquenet

3 个答案:

答案 0 :(得分:1)

使用V3:

$text = @"
    text text text 
    @twitter text text text @facebook 
    text text text @linkedin text text text 
        Hello <a href="http://twitter.com/twitter">@twitter</a> and <a href="http://twitter.com/facebook">@facebook</a> @kiquenet
"@

$regex = [regex]'\s@[^@ ]+\s?'

$regex.matches($text).groups.value

 @twitter 
 @facebook 
 @linkedin 
 @kiquenet

答案 1 :(得分:0)

如果 @username 仅包含字母[a-zA-Z],则 @username 之前/之后为空格,逗号或点[\s,.]和/或标记字符(大于号>,小于号<)然后你应该使用正则表达式:

(?:(?<=[\s,.>])|^)@[a-zA-Z]+(?:(?=[\s,.<])|$)

如果允许其他一些字符,那么必须调整模式,但这个例子应该足够有用......

答案 2 :(得分:0)

使用Split方法:

$text.split() -like "@*"

使用正则表达式:

[regex]::matches($text,'@[^\s|<]+') | foreach {$_.value} | sort -unique
相关问题