我正在使用Python中的正则表达式解决运动问题,并使用Python自动化无聊的东西。问题和我的代码如下。我在Python shell中使用正则表达式,我认为它是正确的,但我不能让它在我的函数中工作,以便它返回正确的答案。
编写一个使用正则表达式的函数,以确保传递的密码字符串很强。强密码定义为长度至少为八个字符,包含大写和小写字符,并且至少有一个数字的密码。您可能需要针对多个正则表达式模式测试字符串以验证其强度。
import re
def pwRegex():
print "Please enter a password"
user_pw = raw_input("> ")
#your_pw = str(user_pw)
passGex = re.compile(r'^(?=.*\d)(?=.*[A-Z])\w{8,15}$')
pass_w = passGex.search(user_pw)
if pass_w != '':
print "Well done"
else:
print "Try again"
pwRegex()
我得到的输出是"再试一次"每次即使我输入的密码都应该通过正则表达式。我尝试制作if语句pass_w == True:
但是我输入的所有内容似乎都通过了正则表达式,即使它不正确。
答案 0 :(得分:1)
在python中进行正则表达式搜索会返回MatchObject,而不是字符串。如果找不到所需的模式,它将返回None
。你应该检查None
,或者更热情地检查真实性
# if pass_w != None: # => less pythonic
if pass_w:
print 'Well done'
else:
print 'Try again'
答案 1 :(得分:0)
import re
print('Please set a new password: ')
def strongpassword():
while True:
password = input()
if lowcase.search(password) == None:
print('The entered password doesn\'t have a lower case character')
continue
if upcase.search(password) == None:
print('The entered password doesn\'t have an upper case character')
continue
if digit.search(password) == None:
print('The entered password doesn\'t have a digit')
continue
if space_8.search(password) == None:
print('The entered password should have atleast 8 characters and no space in between')
continue
else:
print('New Password is Valid and Saved')
break
lowcase = re.compile(r'[a-z]') # this regex searches for atleast one lower case alphabet
upcase = re.compile(r'[A-Z]') # this regex searches for atleast one upper case alphabet
digit = re.compile(r'(\d)') # this regex searches for atleast one digit
space_8 = re.compile(r'^[a-zA-Z0-9]{8,}$') # this regex searches for expressions without any space and atleast 8 characters
strongpassword()
答案 2 :(得分:0)
我猜,它已经不相关了。不过,我想建议一下:
import re
pasword = re.compile(r'''(
\d+ #have at lest one digit
.* # anything
[A-Z]+ # at least 1 capital
.*
[a-z]+ # at least one lower
.*
)''', re.VERBOSE)
# test
pasword.search('1@#A!a').group()
def is_strong_password():
pas = input('Enter a password:')
if len(pas) < 8:
return False
else:
check = pasword.search(''.join(sorted(pas))).group() #sorted is important to be sure that user can write symbols in any order he/she wants
if (not check):
print('Not strong pasword')
return False
else:
print('Strong password')
return True
答案 3 :(得分:0)
我也在做同样的练习,我也在学习 Python。您可以使用 search() 代替 findall()。我只是用它来测试一下。
import re
print('Enter new password')
def strongPW():
while True:
pw = input()
lowercase = [] # lowercase
uppercase = [] # uppercase
digit = [] # number
if len(pw) < 8:
print('your password must have 8 characters or more')
break
# lowercase verification
pwRegex = re.compile(r'[a-z]')
lowercase = pwRegex.search(pw)
if lowercase == []:
print('Your password must have at least one lowercase')
break
# uppercase verification
uppercaseRegex = re.compile(r'[A-Z]')
uppercase = uppercaseRegex.findall(pw)
if uppercase == []:
print('Your password must have at least one uppercase')
break
# digit verification
DigitRegex = re.compile(r'[0-9]')
digit = DigitRegex.findall(pw)
if digit == []:
print('Your password must have at least one digit')
break
else:
print('Your password meets all requirements ')
break
strongPW()