可以输入的字符包括:空格,ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz 0123456789 -
不能输入除前面提到的其它字符,比如* + ! ( ) ? / \ @ # _ ' ` 等
第一个字符不能是空格,并且不能连续超过两个空格
[code="java"]
Pattern p = Pattern.compile("[a-zA-Z0-9-]+([a-zA-Z0-9-]|([ ][a-zA-Z0-9-])+)*");
String[] testStrings = { "a", "a+", " a", "a ", "a asdf", "a -1234" };
for (String str : testStrings) {
Matcher matcher = p.matcher(str);
System.out.println(str + "\t" + matcher.matches());
}
[/code]
[a-zA-Z0-9-]?([a-zA-Z0-9-] {0,2})*