Search code examples
pythonpassword-checker

python Re.search for a password symbol check program (password validation)


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

Solution

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