public class UsernameValidator {
private Pattern pattern;
private Matcher matcher;
private static final String USERNAME_PATTERN ="^[a-z0-9_-][3,15]$";
public UsernameValidator(){
pattern = Pattern.compile(USERNAME_PATTERN);
}
/*
* 验证用户名与正则表达式
* @param用户名用户名进行验证
* @return真正有效的用户名、虚假无效的用户名
*
*/
public boolean validate(final String username){
matcher = pattern.matcher(username);
return matcher.matches();
}
}
转载于:https://blog.51cto.com/10530525/1674409