Open In App

Categorize Password as Strong or Weak using Regex in Python

Last Updated : 17 Nov, 2025
Comments
Improve
Suggest changes
2 Likes
Like
Report

Given a password string, the task is to check whether it is strong or weak based on a set of conditions. A password is considered strong only if it passes all rules below:

  • Length must be between 9 and 20 characters
  • It must not be a space or newline
  • It must not contain the same character repeated 3 or more times consecutively
  • It must not repeat the same substring pattern (minimum length: 2)

For Examples:

Input: Qggf!@ghf3
Output: Strong Password!

Input: aaabnil1gu
Output: Weak Password -> Same character repeats three or more times in a row

Let’s explore different methods to validate password strength.

Using a Single Regex Pattern

This method uses one comprehensive regex pattern that checks password length, repeating characters, and repeating patterns in a single match. If the entire password matches the pattern, it is considered strong.

Python
import re

password = "Qggf!@ghf3"
pattern = (
    r"^(?!.*(.)\1\1)"         
    r"(?!.*(..).*\1)"        
    r"[^\s]{9,20}$"           
)

if re.fullmatch(pattern, password):
    print("Strong Password!")
else:
    print("Weak Password!")

Output
Strong Password!

Explanation:

  • (?!.*(.)\1\1) makes sure no character appears three times back-to-back.
  • (?!.(..).\1) makes sure no two-letter pattern appears again later.
  • [^\s]{9,20} accepts any character except spaces, with length 9–20.
  • re.fullmatch() checks that the entire password follows all rules.

Using Multiple Regex Checks

Instead of one big regex, this method checks each rule separately using smaller regular expressions. If any check fails, the corresponding message is printed.

Python
import re
password = "Qggf!@ghf3"

if password == " " or password == "\n":
    print("Password cannot be a newline or space!")
elif not (9 <= len(password) <= 20):
    print("Password length must be 9-20 characters!")
elif re.search(r"(.)\1\1", password):
    print("Weak Password: Same character repeats three or more times in a row")
elif re.search(r"(..)(.*?)\1", password):
    print("Weak password: Same string pattern repetition")
else:
    print("Strong Password!")

Output
Strong Password!

Explanation:

  • password == " " or password == "\n" detects invalid whitespace passwords.
  • 9 <= len(password) <= 20 ensures length requirement.
  • re.search(r"(.)\1\1", password) searches for any character repeated 3 times.
  • re.search(r"(..)(.*?)\1", password) detects repeating substring pattern of length ≥2.

Manual Character Repetition Check + Regex for Pattern

This method uses a simple loop to detect 3 repeated characters and regex to detect repeating substring patterns. It is slower than full-regex but easier to understand.

Python
import re
password = "aaabnil1gu"

if password == " " or password == "\n":
    print("Password cannot be a newline or space!")
elif not (9 <= len(password) <= 20):
    print("Password length must be 9-20 characters!")
else:
    repeat_three = False
    for i in range(len(password) - 2):
        if password[i] == password[i+1] == password[i+2]:
            repeat_three = True
            break

    if repeat_three:
        print("Weak Password: Same character repeats three or more times in a row")
    elif re.search(r"(..)(.*?)\1", password):
        print("Weak password: Same string pattern repetition")
    else:
        print("Strong Password!")

Output
Weak Password: Same character repeats three or more times in a row

Explanation:

  • password[i] == password[i+1] == password[i+2] checks if any character appears 3 times in a row manually.
  • re.search(r"(..)(.*?)\1", password) detects repeated substring patterns.

Pure Manual Checks Without Built-ins

This method manually checks length, spaces, repeated characters, and substring repetition without relying on regex. It is the slowest and mainly educational.

Python
password = "Qggf!@ghf3"

if password == " " or password == "\n":
    print("Password cannot be a newline or space!")
elif not (9 <= len(password) <= 20):
    print("Password length must be 9-20 characters!")
else:

    # Check repeating characters 3+ times
    repeating_three = False
    for i in range(len(password) - 2):
        if password[i] == password[i+1] == password[i+2]:
            repeating_three = True
            break

    # Check repeating substring patterns
    repeating_pattern = False
    for i in range(len(password) - 3):
        if password[i:i+2] in password[i+2:]:
            repeating_pattern = True
            break

    if repeating_three:
        print("Weak Password: Same character repeats three or more times in a row")
    elif repeating_pattern:
        print("Weak password: Same string pattern repetition")
    else:
        print("Strong Password!")

Output
Strong Password!

Explanation:

  • password[i:i+2] in password[i+2:] manually checks if any 2-character substring repeats later.
  • password[i] == password[i+1] == password[i+2] detects 3 repeated characters.

Explore