python实践项目六:正则表达式-强口令

描述:写一个函数,它使用正则表达式,确保传入的口令字符串是强口令。强口令的定义是:长度不少于8 个字符,  同时包含大写和小写字符, 至少有一位数字。

代码

 1 #!/usr/bin/python
 2 # -*- coding: UTF-8 -*-
 3 # 写一个函数,它使用正则表达式,确保传入的口令字符串是强口令。强口令的定义是:长度不少于8 个字符,
 4 # 同时包含大写和小写字符, 至少有一位数字。你可能需要用多个正则表达式来测试该字符串, 以保证它的强度。
 5 import re,pyperclip
 6 def detection(text):
 7     if (len(text)<8):
 8         return False
 9     number1=re.compile(r'\d+') #创建一个正则表达式:任意数字,r表示不转义,+表示可匹配多个
10     if number1.search(text)==None:
11         return False
12     number2=re.compile(r'[A-Z]+')#任意大写字母
13     if number2.search(text)==None:
14         return False
15     number3 = re.compile(r'[a-z]+')  # 任意小写字母
16     if number3.search(text) == None:
17         return False
18     return True
19 # text=str(pyperclip.paste())#从剪贴板复制命令
20 text=raw_input("Get the password that you want to set:\n")
21 if detection(text):
22     print "The password is the strong password."
23 else:
24     print "Waring:the password is not the strong password!"

运行结果

示例1:

示例2:

示例3:

 

posted on 2019-07-05 17:49  白居不易1101  阅读(1136)  评论(0编辑  收藏  举报