【正则表达式】密码长度为8-16位,数字、字母、字符至少包含两种

【正则表达式】密码长度为8-16位,数字、字母、字符至少包含两种


在网上找了好几个,测试之后都有多多少少的问题,自己一个小时终于憋出来的正则表达,纪念我的处女作!!!

/**
 * 校验密码格式 8-16个字符,不包含空格,必须包含数字,字母或字符至少两种
 *
 * @param value
 * @return
 * @author Super WD
 */
public static boolean checkPasswordFormal(String value) {
    //最接近的
    //String regex = "^(?!^(\\d+|[a-zA-Z]+|[~!@#$%^&*?/]+)$)^[\\w~!@#$%^&*?/]{8,16}$";
    //纯字符不行
    //String regex = "(?!.*\\s)(?!^[0-9]+$)(?!^[A-z]+$)(?!^[^A-z0-9]+$)^[^\\s\\u4e00-\\u9fa5]{8,16}";
    //中文 和 纯字符 不行
    //String regex = "(?!.*\\s)(?!^[\\u4e00-\\u9fa5]+$)(?!^[0-9]+$)(?!^[A-z]+$)(?!^[^A-z0-9]+$)^.{8,16}$";
    //无敌的我 终将走向无敌 Super WD
    String regex = "^(?=.*\\d)(?!^[0-9]+$)(?!^[A-z]+$)(?!^[^A-z0-9]+$)^[^\\s\\u4e00-\\u9fa5]{8,16}";
    return value.matches(regex);
}

我并不觉得枚举是一个不好的测试方法,手动微笑(* ̄︶ ̄)

 public static void main(String[] args) {
        System.out.println(checkPasswordFormal("!@~#$^%&^%#@"));
        System.out.println(checkPasswordFormal("12345678}{|"));
        System.out.println(checkPasswordFormal("A12345678"));
        System.out.println(checkPasswordFormal("12345678:;;"));
        System.out.println(checkPasswordFormal("444412345678"));
        System.out.println(checkPasswordFormal("1234)5678"));
        System.out.println(checkPasswordFormal("、、、、、、、、"));
        System.out.println(checkPasswordFormal("11111111"));
        System.out.println(checkPasswordFormal("bbbbbbbbb"));
        System.out.println(checkPasswordFormal("无1bbbbbbb"));
        System.out.println(checkPasswordFormal("12345678900"));
        System.out.println(checkPasswordFormal("a1234567"));
        System.out.println(checkPasswordFormal(",12345678"));
        System.out.println(checkPasswordFormal("ijhfsshahahah"));
        System.out.println(checkPasswordFormal(".,.,.,,.,.,."));
        System.out.println(checkPasswordFormal("chaojiwudi22ah"));
        System.out.println(checkPasswordFormal(".,.,.,,.,55"));
        System.out.println(checkPasswordFormal("123456781234654687321343513213"));
    }

测试结果
测试结果

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值