我编写了一个代码,它实际上与 Python 中的模式 RegEx 匹配。我已经使用getpass库提示用户输入密码。但是如果用户输入错误的密码[说'HH'-这是无效密码]。一旦用户在输入密码后点击回车,获取通行证正在“ HH" 和 "enter as \r" [两个不同的输入]。现在我需要一种方法来找出如何将 '\r' 与作为输入传递的分开。
我曾尝试使用 strip 函数删除 '\r' 但不成功
import getpass
import re
loopVar=True
while loopVar:
password = getpass.getpass("Enter your password: ")
password.rstrip('\r')
pattern=re.match(r'(?=.{5,32}$)(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[^A-Za-z0-9])',password)
if pattern:
print ("The password mateches as per standards")
loopVar=False
else:
print ("The password "+ password +" is not as per rules ")
Enter your password:
The password hh is not as per rules
Enter your password:(this is taking \r as input character and I dont need this)
The password is not as per rules
Enter your password:[Prompting user to input]