Open In App

Categorize Password as Strong or Weak using Regex in Python

Last Updated : 28 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a password, we have to categorize it as a strong or weak one. There are some checks that need to be met to be a strong password. For a weak password, we need to return the reason for it to be weak. Conditions to be fulfilled are:

  • Minimum 9 characters and maximum 20 characters.
  • Cannot be a newline or a space
  • There should not be three or more repeating characters in a row.
  • The same string pattern(minimum of two character length) should not be repeating.

Note: For checking the basic validations of a password, click here. Examples:

Input1 : Qggf!@ghf3
Output1 : Strong Password!

Input2 : aaabnil1gu
Output2 : Weak Password: Same character repeats three 
or more times in a row

Input3 : Geeksforgeeks
Output3 : Weak Password: Same character repeats three 
or more times in a row

Input4 : Aasd!feasnm
Output4 : Weak password: Same string pattern repetition

Input5 : 772*hdf77
Output5 : Weak password: Same string pattern repetition

Input6 : " "
Output6 : Password cannot be a newline or space!

Below is the implementation. 

Python3




# Categorizing password as Strong or
# Weak in Python using Regex
 
 
import re
 
 
# Function to categorize password
def password(v):
 
    # the password should not be a
    # newline or space
    if v == "\n" or v == " ":
        return "Password cannot be a newline or space!"
 
    # the password length should be in
    # between 9 and 20
    if 9 <= len(v) <= 20:
 
        # checks for occurrence of a character
        # three or more times in a row
        if re.search(r'(.)\1\1', v):
            return "Weak Password: Same character repeats three or more times in a row"
 
        # checks for occurrence of same string
        # pattern( minimum of two character length)
        # repeating
        if re.search(r'(..)(.*?)\1', v):
            return "Weak password: Same string pattern repetition"
 
        else:
            return "Strong Password!"
 
    else:
        return "Password length must be 9-20 characters!"
 
# Main method
def main():
 
    # Driver code
    print(password("Qggf!@ghf3"))
    print(password("Gggksforgeeks"))
    print(password("aaabnil1gu"))
    print(password("Aasd!feasn"))
    print(password("772*hd897"))
    print(password(" "))
 
 
# Driver Code
if __name__ == '__main__':
    main()


Output

Strong Password!
Weak password: Same string pattern repetition
Weak Password: Same character repeats three or more times in a row
Weak password: Same string pattern repetition
Strong Password!
Password cannot be a newline or space!

Time Complexity: O(N)  N is refers to the length of the input password string,
Auxiliary Space: O(1) 



Previous Article
Next Article

Similar Reads

Generating Strong Password using Python
Having a weak password is not good for a system that demands high confidentiality and security of user credentials. It turns out that people find it difficult to make up a strong password that is strong enough to prevent unauthorized users from memorizing it. This article uses a mixture of numbers, alphabets, and other symbols found on the computer
3 min read
Python | Categorize the given list by string size
Sometimes, we have a use case in which we need to perform the grouping of strings by various factors, like first letter or any other factor. These type of problems are typical to database queries and hence can occur in web development while programming. This article focuses on one such grouping by size of string. Let's discuss certain ways in which
6 min read
Python | Categorize tuple values into dictionary value list
Sometimes, while working with Python, we might face a problem in which we have data in form of list of tuples, and what we intend is, to categorize them into dictionary with a value list of tuples. Let's discuss certain ways in which this task can be performed. Method #1 : Using setdefault() + loop This task can easily be performed using setdefault
6 min read
Weak References in Python
Python's memory management algorithm uses a reference counting mechanism for garbage collection, which tracks all reachable objects. All Python objects include a reference count field, which counts how many objects are referencing it. If an object is referenced by another object, then its counter is set to a non-zero number and the object can't be
4 min read
Pafy - Getting Weak Reference of Stream
In this article, we will see how we can get weak reference if available for the given youtube video stream in pafy. Pafy is a python library to download YouTube content and retrieve metadata. Pafy object is the object which contains all the information about the given video. Stream is basically available resolution of video is available on youtube.
2 min read
Validate an IP address using Python without using RegEx
Given an IP address as input, the task is to write a Python program to check whether the given IP Address is Valid or not without using RegEx. What is an IP (Internet Protocol) Address? Every computer connected to the Internet is identified by a unique four-part string, known as its Internet Protocol (IP) address. An IP address (version 4) consists
2 min read
Python program to find all Strong Numbers in given list
Given a list, write a Python program to find all the Strong numbers in a given list of numbers. A Strong Number is a number that is equal to the sum of factorial of its digits. Examples: Input : [1, 2, 5, 145, 654, 34] Output : [1, 2, 145] Input : [15, 58, 75, 675, 145, 2] Output : [145, 2] Explanation : We defined 2 functions here: First is factor
6 min read
Python program to print all Strong numbers in given list
Given a list, write a Python program to print all the strong numbers in that list. Strong Numbers are the numbers whose sum of factorial of digits is equal to the original number. Example for checking if number is Strong Number or not. Input: n = 145 Output: Yes Explanation: Sum of digit factorials = 1! + 4! + 5! = 1 + 24 + 120 = 145 Steps for chec
5 min read
Find all the patterns of “1(0+)1” in a given string using Python Regex
A string contains patterns of the form 1(0+)1 where (0+) represents any non-empty consecutive sequence of 0’s. Count all such patterns. The patterns are allowed to overlap. Note : It contains digits and lowercase characters only. The string is not necessarily a binary. 100201 is not a valid pattern. Examples: Input : 1101001 Output : 2 Input : 1000
2 min read
Python | Program that matches a word containing 'g' followed by one or more e's using regex
Prerequisites : Regular Expressions | Set 1, Set 2 Given a string, the task is to check if that string contains any g followed by one or more e's in it, otherwise, print No match. Examples : Input : geeks for geeks Output : geeks geeks Input : graphic era Output : No match Approach : Firstly, make a regular expression (regex) object that matches a
2 min read
Practice Tags :