社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

Python正则表达式匹配字母表中的3个连续字符,但不一定并排

CitronWorld • 4 年前 • 1646 次点击  

我必须构建一个python函数,它使用正则表达式来检查密码的复杂性,密码应该至少有8个字符,包含数字、字母和特殊符号,以及最后的规则(最难的规则):不包含三个连续的字母或三个连续的数字。 为了说明最后一条规则: Mz2a%yc98B 不接受,因为它包含3个连续字母:aBc

前两条规则我做得很好,但我不知道最后一条规则我能做什么。我觉得这很复杂。有人告诉我,用正则表达式是不可能做到的,但实际上这必须是可能的,否则就太疯狂了不?

非常感谢你!

到目前为止,我的代码是:

import re

pattern = re.compile(r"")
while True:
    password = input("Enter your password")
    if re.match(r"(?=.*[a-z])(?=.*[A-Z])(?=(.*[!%@$#&^*()\-__+.]){1,})(?=.*[0-9]).{8,}", password):
        result = pattern.match(password)
        print("Your password is strong enough")
    else:
        print("Your password is not strong enough")

在我给出的密码示例中,它们是aBc,B是最后一个字符。 这只是一个例子。当我指的是连续,我指的是字母表中的连续。例如,如果使用efg字母,甚至在密码中散布,它必须检测到它。 谢谢

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/133664
文章 [ 2 ]  |  最新文章 4 年前
MattDMo
Reply   •   1 楼
MattDMo    4 年前

以下是你的问题集的第三个标准的解决方案。

import string
from more_itertools import windowed

alphabet = string.ascii_lowercase
numbers = string.digits

passwords = ["Mz2a%yc98B", "a1b2g3D!", "Tr0ub4dor?"]

def find_triplets(charset: set, target: str) -> bool:
    if len(charset) < 3:
        return False
    else:
        chars = "".join(sorted(charset))
        triplets = ["".join(tpl) for tpl in windowed(chars, 3)]
        return any(triplet in target for triplet in triplets)

for pwd in passwords:
    # we use sets to get rid of duplicatess
    alpha = set()
    nums = set()
    for char in pwd:
        if char.isalpha():
            alpha.add(char.lower())
        elif char.isdigit():
            nums.add(char)
        else:
            pass # we don't care about symbols here
    if find_triplets(alpha, alphabet) or find_triplets(nums, numbers):
        print(f"Your password {pwd} contains 3 consecutive letters or numbers")
    else:
        print(f"Your password {pwd} is good enough, I guess")

在设置代码之后,我们首先定义一个函数来生成3个字符的字符串( triplets )从输入 charset ,然后将其排序并转换为字符串本身。这使用了 windowed() 来自非常有用的第三方的功能 more_itertools 单元函数返回 True 如果它在序列中找到任何匹配连续序列的三胞胎 target 绳子,还有 False 如果没有,那就简单了。

接下来,我们重复我们的密码列表(您使用的 input() 在代码中,这很好)。首先,我们逐个字符迭代密码字符串,并将它们添加到适当的集合中(字母字符的情况下使用小写)。然后我们打电话给 find_triplets() 函数测试字母或数字的连续运行,并相应地打印消息。

很明显,你会想改变一些,以适应你的任务,但我会让你自己决定。

还有一种方法是使用 re.search() 但是我认为 any(triplet in target for triplet in triplets) 更像是蟒蛇。

Cary Swoveland
Reply   •   2 楼
Cary Swoveland    4 年前

你问是不是 可能的 要用正则表达式实现这一点?当然它漂亮吗?这在旁观者的眼里。

您需要一个如下所示的正则表达式(设置大小写无关标志):

^(?=.*\d)(?=.*[a-z])(?=.*[<special symbols here>])(?!<no 3 digits that are consecutive>)(?!<no three letters that are consecutive>).{8}

让我们看看 消极前瞻

(?!<no 3 digits that are consecutive>) 

我们可以这样写。

(?!(?:(?=.*0)(?=.*1)(?=.*2))|(?:(?=.*1)(?=.*2)(?=.*3))|(?:(?=.*2)(?=.*3)(?=.*4))|(?:(?=.*3)(?=.*4)(?=.*5))|(?:(?=.*4)(?=.*5)(?=.*6))|(?:(?=.*5)(?=.*6)(?=.*7))|(?:(?=.*6)(?=.*7)(?=.*8))|(?:(?=.*7)(?=.*9)(?=.*9)))

Demo

这个表达式可以用英文写成 冗长的 模式( re.X 或 re.VERBOSE )让它自我记录。

(?!          # begin negative lookahead       
  (?:        # begin non-capture group
    (?=.*0)  # match > 0 characters followed by 0 (positive lookahead) 
    (?=.*1)  # match > 0 characters followed by 1 
    (?=.*2)  # match > 0 characters followed by 2
  )          # end non-capture group
  |          # or
  ... similar for (?:(?=.*1)(?=.*2)(?=.*3))
  ...
)            # end negative lookahead

建筑

(?!<no three letters that are consecutive>)

相似(包含23个元素的交替, a , b 和 c 到 x , y 和 z ).