//已修改!
//匹配@xxx,长度为1-20。
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MatcherDemo {
private static String regex = "@[^\\\\@ ]{1,20}";
private static Pattern pattern = Pattern.compile(regex);
public static void main(String[] args) throws Exception {
printNames(matcherNames("@aaaaa@cccccc"));;
printNames(matcherNames("@bbbb @dddddd"));
}
private static String[] matcherNames(String input) {
List
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
list.add(matcher.group());
}
return list.toArray(new String[0]);
}
private static void printNames(String[] names) {
System.out.printf("共匹配到%s个用户:\n", names.length);
for (int i = 0; i < names.length; i++) {
System.out.printf("用户%s是:%s\n", i + 1, names[i]);
}
}
}
^@[^\\@\s]{1,20}
验证如何?
@cccccc @bbbbb
/@a*/你试一下,不过那个重复1到20遍不知道你什么意思,是重复匹配还是重复输出啊
很简单:@\w+\b