鱼C论坛

 找回密码
 立即注册
查看: 1706|回复: 1

[已解决]正则表达式,求大神

[复制链接]
发表于 2022-4-7 09:10:37 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
写一个函数,它使用正则表达式,确保传入的口令字符串是强口令。强口令的
定义是:长度不少于 8 个字符,同时包含大写和小写字符,至少有一位数字。你可
能需要用多个正则表达式来测试该字符串,以保证它的强度。
最佳答案
2022-4-7 09:25:13
  1. import re

  2. def test(s):
  3.     if len(s) < 8:
  4.         return False                              
  5.     if not(re.search(r"[a-z]", s) and re.search(r"[A-Z]", s)):
  6.         return False
  7.     if not re.search(r"\d", s):
  8.         return False
  9.     return True
  10.                                              
  11. strings = ["asxd", "Abuhgbnj", "Afgggh85","12345678"]

  12. for i in strings:
  13.     if test(i):
  14.         print(f"{i}是强口令")
  15.     else:                                             
  16.         print(f"{i}不是强口令")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-4-7 09:25:13 | 显示全部楼层    本楼为最佳答案   
  1. import re

  2. def test(s):
  3.     if len(s) < 8:
  4.         return False                              
  5.     if not(re.search(r"[a-z]", s) and re.search(r"[A-Z]", s)):
  6.         return False
  7.     if not re.search(r"\d", s):
  8.         return False
  9.     return True
  10.                                              
  11. strings = ["asxd", "Abuhgbnj", "Afgggh85","12345678"]

  12. for i in strings:
  13.     if test(i):
  14.         print(f"{i}是强口令")
  15.     else:                                             
  16.         print(f"{i}不是强口令")
复制代码

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +1 收起 理由
python爱好者. + 5 + 5 + 1

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-29 08:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表