需求是需要校验支付密码,条件:不能为连续或重复数字(6位),不能包含手机号码或者身份证号码的一部分。写了一个工具类:
/**
* @Description 支付密码校验
* @param password 支付密码
* @param text 文本
* @return 符合 true 不符合 false
*/
public static boolean passwordVerification(String password, String text){
//数字不是连续的
String res = "^(?:(\\d)(?!((?<=9)8|(?<=8)7|(?<=7)6|(?<=6)5|(?<=5)4|(?<=4)3|(?<=3)2|(?<=2)1|(?<=1)0){5})(?!\1{5})(?!((?<=0)1|(?<=1)2|(?<=2)3|(?<=3)4|(?<=4)5|(?<=5)6|(?<=6)7|(?<=7)8|(?<=8)9){5})){6}$";
//数字不是重复的
String reg = "^(?=.*\\d+)(?!.*?([\\d])\\1{5})[\\d]{6}$";
if(!Pattern.matches(res,password) || !Pattern.matches(reg,password) || text.contains(password)){
return false;
}else{
return true;
}
}
使用方式:password
支付密码,text
就是手机号码或者身份证号码,条件符合返回true
、不符合返回false
。