Program to check whether the given password is strong or weak using regex

Introduction

The task is to check whether the given password is strong or not using regular expression.

Requirements for strong password:

  • The password must be 8 to 15 characters long.
  • The password must contain at least one lowercase, uppercase alphabet, digit, and a special character.
  • The password must not contain a newline or space in between the string.
  • Same character must not be repeated more than 3 times.
  • There must not be the repetition of string pattern.

check whether the given password is strong

Program

import re 
def check(pwd): 
    if pwd == "\n" or pwd == " ": 
        return "Password must not contain a newline or space!"
 
    if 8 <= len(pwd) <= 15: 
        if not re.search('[a-z]', pwd):
            print("Weak Password! At least one lowercase alphabet should be present!")
        elif not re.search('[A-Z]', pwd): 
            print("Weak Password! At least one uppercase alphabet should be present!") 
        elif not re.search('[0-9]', pwd): 
            print("Weak Password! At least one digit should be present!")
        elif not re.search('[_@$]', pwd): 
            print("Weak Password! At least one special char should be present!")
        elif re.search(r'(.)\1\1\1', pwd): 
            print("Weak Password! Char repetition more than 3 times!")
        elif re.search(r'(..)(.*?)\1', pwd): 
            print("Weak Password! String pattern repetition!")
        else: 
            print("The given password is a strong password!")
   
    else: 
        print("Weak Password! Password must be 8 to 15 characters long! ")
        
ip_str = input("Enter the string: ")
check(ip_str)

Output

check whether the given password is strong Output

Explanation

In the above python code, we have checked the conditions for strong password using re.search() method in if-elif condition. If all the conditions are satisfied, the given password is strong. In case any of the condition fails, the password is weak.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.