Thursday, February 29, 2024
HomeSoftware EngineeringMethods to Validate Passwords with Regex in Python

Methods to Validate Passwords with Regex in Python


The problem

It’s essential write regex that can validate a password to verify it meets the next standards:

  • No less than six characters lengthy
  • comprises a lowercase letter
  • comprises an uppercase letter
  • comprises a quantity

Legitimate passwords will solely be alphanumeric characters.

The answer in Python code

Possibility 1:

from re import compile, VERBOSE

regex = compile("""
^              # start phrase
(?=.*?[a-z])   # at the very least one lowercase letter
(?=.*?[A-Z])   # at the very least one uppercase letter
(?=.*?[0-9])   # at the very least one quantity
[A-Za-zd]     # solely alphanumeric
{6,}           # at the very least 6 characters lengthy
$              # finish phrase
""", VERBOSE)

Possibility 2:

import re
regex = re.compile(r"""
    (?=^[a-zA-Z0-9]{6,}$)
    (?=w*[a-z])
    (?=w*[A-Z])
    (?=w*d)
""", re.X)

Possibility 3:

regex="^(?=.*[a-z])(?=.*[A-Z])(?=.*d)[^W_]{6,}$"

Check instances to validate our resolution

from re import search
take a look at.describe("Fundamental checks")
take a look at.assert_equals(bool(search(regex, 'fjd3IR9')), True)
take a look at.assert_equals(bool(search(regex, 'ghdfj32')), False)
take a look at.assert_equals(bool(search(regex, 'DSJKHD23')), False)
take a look at.assert_equals(bool(search(regex, 'dsF43')), False)
take a look at.assert_equals(bool(search(regex, '4fdg5Fj3')), True)
take a look at.assert_equals(bool(search(regex, 'DHSJdhjsU')), False)
take a look at.assert_equals(bool(search(regex, 'fjd3IR9.;')), False)
take a look at.assert_equals(bool(search(regex, 'fjd3  IR9')), False)
take a look at.assert_equals(bool(search(regex, 'djI38D55')), True)
take a look at.assert_equals(bool(search(regex, 'a2.d412')), False)
take a look at.assert_equals(bool(search(regex, 'JHD5FJ53')), False)
take a look at.assert_equals(bool(search(regex, '!fdjn345')), False)
take a look at.assert_equals(bool(search(regex, 'jfkdfj3j')), False)
take a look at.assert_equals(bool(search(regex, '123')), False)
take a look at.assert_equals(bool(search(regex, 'abc')), False)
take a look at.assert_equals(bool(search(regex, '123abcABC')), True)
take a look at.assert_equals(bool(search(regex, 'ABC123abc')), True)
take a look at.assert_equals(bool(search(regex, 'Password123')), True)

take a look at.describe("Random checks")
from random import random, randint
sol=r"^(?=.*[a-z])(?=.*[A-Z])(?=.*d)[a-zA-Zd]{6,}$"
decrease="abcdefghijklmnopqrstuvwxyz"
higher="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
digits="0123456789"
all="".be part of([lower,upper,digits])
fallacious=" !_+-?/"

for _ in vary(100):
  s="".be part of(sorted([upper[randint(0,len(upper)-1)], decrease[randint(0,len(lower)-1)], digits[randint(0,len(digits)-1)]]+[all[randint(0,len(all)-1)] if randint(0,10) else fallacious[randint(0,len(wrong)-1)] for q in vary(randint(0,15))], key=lambda a: random()))
  take a look at.it("Testing for "+repr(s))
  take a look at.assert_equals(search(regex,s)!=None, search(sol,s)!=None, "It ought to work for random inputs too")
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments