您的位置:首页 > 编程语言 > Python开发

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

2019-07-05 17:49 281 查看

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

代码

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

运行结果

示例1:

示例2:

示例3:

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: