VOOZH about

URL: https://www.geeksforgeeks.org/python/categorize-password-as-strong-or-weak-using-regex-in-python/

⇱ Categorize Password as Strong or Weak using Regex in Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Categorize Password as Strong or Weak using Regex in Python

Last Updated : 17 Nov, 2025

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.


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.


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.


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.


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.
Comment
Article Tags: