-2

我想在字符串中提取与“@”对应的单词并将其保存在列表中:

例如:

 "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'(?<=@)\w+', text)

以上用于解决问题..但我在java中不太确定。谢谢

4

1 回答 1

1

Matcher。您需要遍历匹配项,例如:

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

输出:

crazy_boy
foobar
于 2013-05-31T23:14:40.097 回答