1

I'm making a program to check password strength. The password must have uppercase letters, lowercase letters, allowed symbols and numbers. I have been able to get the rest of the program to work(useing the re.search(r'[a-z],password structure) I shortened the problem area to these few lines but cannot get this part to work. Should I be using something different to re.search?

    import re
    symbols = ["!","(",")","£","^"]
    password = input("password")
    if re.search(r'[symbols]',password):
      print("ok")
    else:
      print("no")
elise
  • 13
  • 2
  • You can't just expect random names to get interpolated into your string. Try something like `'[{}]'.format(symbols)`. – jonrsharpe Nov 05 '17 at 17:24

2 Answers2

0

You're almost there. Just specify all your symbols inside the regex range:

password_valid = bool(re.search(r'[!()£^]', password))

Inside [..] most regex metacharacters lose their special meaning, just watch out for the ^ in the first position and for the character classes, like \w.

If you take care to prevent those cases, you can keep symbols in the list, and use it like:

symbols = ["!", "(", ")", "£", "^"]
password_valid = bool(re.search(r'[{}]'.format(''.join(symbols)), password))
randomir
  • 17,989
  • 1
  • 40
  • 55
0

Complete solution could be something like this

import re
def is_password_valid(password):
    return bool(re.search('(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[+\-*\/])(?=.*[!()£^])', password))

or simpler to understand

import re
def is_password_valid(pas):
    is_ok1 = bool(re.search("[a-z]",pas))
    is_ok2 = bool(re.search("[A-Z]",pas))
    is_ok3 = bool(re.search("[0-9]",pas))
    is_ok4 = bool(re.search("[+\-*\/!()£^]",pas))        
    return is_ok1 and is_ok2 and is_ok3 and is_ok4 
Sergey Anisimov
  • 323
  • 1
  • 4
  • 10