某软件密码设置规则的不同语言实现步骤比较

先提密码规则:1、密码长度为8-15位;2、需包含特殊字符(字母与数字外的字符均为特殊字符);3、包含大小写字母、数字中的两种或以上;4、不允许有空格。小试牛刀了下

java:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegularExpression {
    public static void main(String[] args) {
        String text = "123abcderfg@";
        String special_str = "[^0-9a-zA-Z]";
        String upper_str = "[A-Z]";
        String lower_str = "[a-z]";
        String digit_str = "[0-9]";
        String space_str = " ";
        int l = text.length();

        Pattern p1 = Pattern.compile(special_str);
        Matcher m1 = p1.matcher(text);
        boolean b1 = m1.find();

        Pattern p2 = Pattern.compile(upper_str);
        Matcher m2 = p2.matcher(text);
        boolean b2 = m2.find();

        Pattern p3 = Pattern.compile(lower_str);
        Matcher m3 = p3.matcher(text);
        boolean b3 = m3.find();

        Pattern p4 = Pattern.compile(digit_str);
        Matcher m4 = p4.matcher(text);
        boolean b4 = m4.find();

        Pattern p5 = Pattern.compile(space_str);
        Matcher m5 = p5.matcher(text);
        boolean b5 = m5.find();

        if(((b2 & b3)||(b2 & b4)||(b3 & b4)||(b2 & b3 & b4)) & b1 & !b5 & l>=8 & l<=15){
            System.out.println("这个密码可以了");
        }
    }
}

python:

import re

text =  "123abcderfg@"

length = len(text)
special_str = re.search("[^0-9a-zA-Z]",text)
upper_str = re.search("[A-Z]",text)
lower_str = re.search("[a-z]",text)
digit_str = re.search("[0-9]",text)
space_str = re.search(" ",text)

if ((upper_str and lower_str) or (upper_str and digit_str) or (lower_str and digit_str) or (upper_str and lower_str and digit_str)) and special_str and space_str is None and length>=8 and length<=15:
    print("这个密码可以了")

haha,难怪有不少人会更喜欢python(不!php天下第一);时间紧张 代码可能有错 读者可以自行验证,代码也有可以优化的地方,如适当用更复杂点的正则表达式进行判断等

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值