先提密码规则: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天下第一);时间紧张 代码可能有错 读者可以自行验证,代码也有可以优化的地方,如适当用更复杂点的正则表达式进行判断等