有两种写法,但代码其实是一样的
1、使用String自带的matches的验证
String passwd = "rqbkjhrjqh1111@!45";
String regex = "^[A-Za-z]|[0-9]|[!@#$%^&*]{6,18}$";
if(!passwd .matches(regex)){
return false; //验证不通过
}
解析:这种方式其实我们看源代码使用的是如下方式
2、使用Pattern和Matcher来进行验证
String passwd = "rqbkjhrjqh1111@!45";
String regex = "^[A-Za-z]|[0-9]|[!@#$%^&*]{6,18}$";
Pattern pattern = Pattern.compile(regex); // 编译正则表达式
Matcher matcher = pattern.matcher(passwd);
if (!matcher.matches()) {
return false;
}
解析:这种方式其实和第一种方式是一样的,我们看第一种的String中源代码就可以发现是一致的
版权声明:本文为m0_53455309原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。