2013-05-31 26 views
-2

我想提取的字符串對應於「@」字樣,並將其保存在一個列表:Twitter的風格正則表達式匹配

例如:

"This is @crazy_boy crazy" should give ["crazy_boy"] 

"This is @crazy_boy crazy @foobar" should give ["crazy_boy","foobar"] 

"This statement is boring" should give [] //or whatever is an empty list 

在蟒蛇

targets = re.findall(r'(?<[email protected])\w+', text) 

上面的使用做的伎倆..但我不是很確定在java中。 感謝

回答

1

Matcher。你需要通過比賽來循環,例如:

String input = "This is @crazy_boy crazy @foobar"; 
Matcher matcher = Pattern.compile("(?<[email protected])\\w+").matcher(input); 
while (matcher.find()) 
    System.out.println(matcher.group()); 

輸出:

crazy_boy 
foobar