带有lookbehind断言的密码

发布于 2025-01-18 18:02:13 字数 488 浏览 2 评论 0原文

您好,我正在开始使用正则表达式,我已经面临寻找解决方案的问题,以编写使用后向断言接受密码的代码...

`import re
   e = re.compile(r"[A-Za-z\d]((?<=[A-Z]).*)((?<=[a-z]).*)((?<=\d).*)")

while(True):
  while(True):
    password= input("Enter Password: ")
    print(password)
    result = e.search(password)
    print(result)

    if result is not None:
        print("Correct")
        break
    print("False")`

如果我输入以大写字符开头的密码,后跟小写字符和一个,它会接受任何输入数字。但是,它拒绝任何以数字开头的密码。 我的问题:使用后行断言时是否应该遵循某种顺序模式?

Hi I am getting started with regex and I am already facing a problem with finding solutionto write a code that accepts password using lookbehind Assertion...

`import re
   e = re.compile(r"[A-Za-z\d]((?<=[A-Z]).*)((?<=[a-z]).*)((?<=\d).*)")

while(True):
  while(True):
    password= input("Enter Password: ")
    print(password)
    result = e.search(password)
    print(result)

    if result is not None:
        print("Correct")
        break
    print("False")`

It accept any input if I enter A password that starts with an Uppercase caracter followed by lowercase caracter and one digit. However, it refuses Any password that starts with a digit.
My question: is there an order pattern that i should follow while using lookbehind assertions ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

≈。彩虹 2025-01-25 18:02:14

如果您想像您所描述的那样匹配 1Hello1hEllo 等密码,您可以只选择

e = re.compile(r"\w+")

至少包含一个字符的密码,或者选择

e = re.compile(r"\w{8}\w*")

length > 的密码;= 8

\w 代表任何字母数字字符 (文档

If you want to match passwords like 1Hello and 1hEllo like you described, you could go for just

e = re.compile(r"\w+")

for passwords with at least one character or with

e = re.compile(r"\w{8}\w*")

for passwords of length >= 8

\w stands for any alphanumeric character (documentation)

残月升风 2025-01-25 18:02:14

在这种情况下,我会发现任何老师都要求看起来像个奇迹是很奇怪的。

最明显的方法是 lookahead

^(?=.*[A-Z])(?=.*[a-z])(?=.*\d).{6,}

您甚至可以无需查看即有效的)。

^(.{,5}|[^A-Z]*|[^a-z]*|\D*)$

那么Beebehind呢?我可以尝试扭转我的初步尝试:

.{6,}(?<=[A-Z].*)(?<=[a-z].*)(?<=\d.*)$

这在JavaScript中起作用,但在Python中不能:
Python的Regex Engine要求固定宽度 lookBehind。

哦,当然,我可以介绍一个看起来像个外观。你去。

^(?=.*[A-Z])(?=.*[a-z])(?=.*\d).{6,}(?<=...)

但这没有目的。
我只是为了在我的正则表达式中添加lookhind。
不是因为它带来任何好处。

我一直认为看起来是我的最后一个手段,以防其他一切都失败或导致荒谬的重音。
在这里,绝对不是这样。

I would find it strange for any teacher to demand lookbehind in this case.

The most obvious approach is lookahead:

^(?=.*[A-Z])(?=.*[a-z])(?=.*\d).{6,}

You can even do it without lookaround, but then you'd better make it a negative test (i.e. a regex matching the invalid passwords rather than the valid ones).

^(.{,5}|[^A-Z]*|[^a-z]*|\D*)$

So what about lookbehind? I could try and reverse my initial attempt:

.{6,}(?<=[A-Z].*)(?<=[a-z].*)(?<=\d.*)$

This works in JavaScript, but not in Python:
Python's regex engine demands fixed-width lookbehind.

Oh sure, I could introduce a lookbehind. There you go.

^(?=.*[A-Z])(?=.*[a-z])(?=.*\d).{6,}(?<=...)

But it serves no purpose.
I'm just adding lookbehind for the sake of having lookbehind in my regex;
not because it brings any benefit.

I've always considered lookbehind to be my last resort, in case everything else fails or results in a ridiculously big regex.
Here, that is definitely not the case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文