带有lookbehind断言的密码
您好,我正在开始使用正则表达式,我已经面临寻找解决方案的问题,以编写使用后向断言接受密码的代码...
`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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想像您所描述的那样匹配
1Hello
和1hEllo
等密码,您可以只选择至少包含一个字符的密码,或者选择
length > 的密码;= 8
\w
代表任何字母数字字符 (文档)If you want to match passwords like
1Hello
and1hEllo
like you described, you could go for justfor passwords with at least one character or with
for passwords of
length >= 8
\w
stands for any alphanumeric character (documentation)在这种情况下,我会发现任何老师都要求看起来像个奇迹是很奇怪的。
最明显的方法是 lookahead :
您甚至可以无需查看即有效的)。
那么Beebehind呢?我可以尝试扭转我的初步尝试:
这在JavaScript中起作用,但在Python中不能:
Python的Regex Engine要求固定宽度 lookBehind。
哦,当然,我可以介绍一个看起来像个外观。你去。
但这没有目的。
我只是为了在我的正则表达式中添加lookhind。
不是因为它带来任何好处。
我一直认为看起来是我的最后一个手段,以防其他一切都失败或导致荒谬的重音。
在这里,绝对不是这样。
I would find it strange for any teacher to demand lookbehind in this case.
The most obvious approach is lookahead:
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).
So what about lookbehind? I could try and reverse my initial attempt:
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.
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.