0

I'm working on an exercise problem from the regex chapter in 'Automate the boring stuff with Python' the question and my code is below. I got the regex working in the Python shell and I think its correct but I just can't get it to work within my function so that it returns the correct answer.

Write a function that uses regular expressions to make sure the password string it is passed is strong. A strong password is defined as one that is at least eight characters long, contains both uppercase and lowercase characters, and has at least one digit. You may need to test the string against multiple regex patterns to validate its strength.

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()

the output I get is "Try again" every time even when I'm entering a password that should pass the regex. I tried making the if statement pass_w == True: but then everything that I entered seemed to pass the regex even if incorrect.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
easy_c0mpany80
  • 327
  • 1
  • 7
  • 18
  • Note: just to add that a password must be 8-15 characters with at least 1 digit and 1 uppercase letter for it to pass – easy_c0mpany80 Nov 10 '15 at 02:45
  • 2
    Have you tried poor man's debugging by printing out pass_w? I'm quite sure it is not a string object. – nhahtdh Nov 10 '15 at 02:46
  • 1
    Your regex fails to require lowercase, and needlessly imposes a maximum length. Limiting the length of passwords is anathema to sane security practices. – tripleee Nov 10 '15 at 04:12
  • @triplee, thanks for the feedback, the max length is just something I decided to add myself just for practice with regex, noted for the future though! – easy_c0mpany80 Nov 10 '15 at 09:17
  • You may benefit from [Reference - Password Validation](https://stackoverflow.com/questions/48345922/reference-password-validation/) – ctwheels Mar 28 '18 at 13:59

4 Answers4

1

Regex searching in python gives back a MatchObject, not a string. If it does not find the required pattern, it will return None. You should check for None, or, more pythonically, check for truthiness

# if pass_w != None: # => less pythonic
if pass_w:
    print 'Well done'
else:
    print 'Try again'
Strikeskids
  • 3,932
  • 13
  • 27
1
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()
1

I guess, it is not relevant anymore. However, I would like to suggest this:

  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
user13
  • 351
  • 3
  • 17
0

I was also working on the same exercise, I'm learning python as well. You can search() instead of findall(). i just used it just to test it out.

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()
Dharman
  • 30,962
  • 25
  • 85
  • 135
ll ll
  • 46
  • 3