7.18.1 强口令检测
import re
def pwdcheck(pwd):
strpc = "你的密码"
pwdn = 5
numbercheck = re.compile(r'\d').search(pwd)
uppercheck = re.compile(r'[A-Z]').search(pwd)
lowercheck = re.compile(r'[a-z]').search(pwd)
othercheck = re.compile(r'[!@#$%^&\*()_+~\=\-\[\]\\\{\}\|\;\'\:\"\,\.\/\<\>\?]').search(pwd)
if len(pwd) >= 8:
pwdn -= 1
else:
strpc += "小于8位,"
if numbercheck != None:
pwdn -= 1
else:
strpc += "未包含数字,"
if uppercheck != None:
pwdn -= 1
else:
strpc += "未包含大写字母,"
if lowercheck != None:
pwdn -= 1
else:
strpc += "未包含小写字母,"
if othercheck != None:
pwdn -= 1
else:
strpc += "未包含特殊字符,"
if pwdn == 0:
print(strpc + "符合所有安全强度要求,如果可以使用中文,请使用中文")
else:
print(strpc + "将重新检测密码强度.")
setpwd()
def setpwd():
pwd =input("请输入8位以上密码其中包含大小写字母,数字及特殊字符:\n")
pwdcheck(pwd)
7.18.2 strip()的正则表达式版本
import re
def re_strip(msg,msgrp=None):
if msgrp is None:
regex = re.compile(r'^[\s]*|[\s]*$')
else:
regex = msgrp
return re.sub(regex,"",msg)
a=" \t \n 112321233123 \t \n"
re_strip(a)
re_strip(a,"123")