Skip to content Skip to sidebar Skip to footer

Match Password With Python Regex

The requirements for my password are: at least 10 symbols at least one digit at least one uppercase letter at least one lowercase letter only letters or digits So I want to use t

Solution 1:

Yep, you could do this through regex.

^(?=.*?\d)(?=.*?[A-Z])(?=.*?[a-z])[A-Za-z\d]{10,}$

DEMO

>>>import re>>>m = re.compile(r'^(?=.*?\d)(?=.*?[A-Z])(?=.*?[a-z])[A-Za-z\d]{10,}$')>>>m.match('43543fooR')>>>m.match('43543fooRy')
<_sre.SRE_Match object; span=(0, 10), match='43543fooRy'>
>>>m.match('foobar7678A')
<_sre.SRE_Match object; span=(0, 11), match='foobar7678A'>
>>>m.match('foobar76(')>>>m.match('fokhjf7645464644sresrtf')>>>m.match('fokhjf764546M4644sresrtf')
<_sre.SRE_Match object; span=(0, 24), match='fokhjf764546M4644sresrtf'>
>>>
  • (?=.*?\d) Checks for atleast one digit
  • (?=.*?[A-Z]) Atleast one uppercsae.
  • (?=.*?[a-z]) Atleast one lowercase.
  • [A-Za-z\d]{10,} Matches uppercase or lowercase or digit characters 10 or more times. This ensures that the match must have atleast 10 characters.

Solution 2:

whileTrue:
    passw = input("password: ")
    iflen(passw)<10:
        print ("not enough")
        continueif passw.isalpha():
        print ("atleast 1 digit")
        continueif passw.islower():
        print ("at least 1 uppercase")
        continueifpass.isupper():
        print ("Do you even password?")
        continueelse:
        print ("Finally, you created a password that FBI using.")

Output;

>>> 
password: a
not enough
password: asdasdasdasdasdadsadsadsads
atleast 1 digit
password: asdasdasdasdasdasdasdasdads1111
at least 1 uppercase
password: asASasdasdasdasdasdas111
Finally, you created a password that FBI using.
password:

Use if statements, check the lenght first. Then check is there any digit with isalpha(). Then check is there any uppercase with islower(). You don't need regex.

Solution 3:

Here is a similar article that answers most of your question How to test a regex password in Python?

You might also find the regular expression cookbook helpful https://www.safaribooksonline.com/library/view/regular-expressions-cookbook/9781449327453/ch04s19.html

Live Demo: https://ideone.com/4PGlTu

import re

password = raw_input("Enter string to test: ")

atLeastTenCharacters = ".{10,}"
hasAtLeastTenCharacters = re.findall(atLeastTenCharacters, password)
if (not hasAtLeastTenCharacters):
    print "Password should be at least 10 characters long!"

atLeastOneDigit = "[0-9]"
hasAtLeastOneDigit = re.findall(atLeastOneDigit, password)
if (not hasAtLeastOneDigit):
    print "Password should have at least one digit!"

atLeastOneUpper = "[A-Z]"
hasAtLeastOneUpper = re.findall(atLeastOneUpper, password)
if (not hasAtLeastOneUpper):
    print "Password should have at least one upper case character!"

atLeastOneLower = "[a-z]"
hasAtLeastOneLower = re.findall(atLeastOneLower, password)
if (not hasAtLeastOneLower):
    print "Password should have at least one lower case character!"

specialCharacters = "[^A-Za-z0-9]"
hasSpecialCharacters = re.findall(specialCharacters, password)
if (hasSpecialCharacters):
    print "Password should NOT have special characters!"

if (hasAtLeastTenCharacters and hasAtLeastOneDigit and hasAtLeastOneUpper and hasAtLeastOneLower and (not hasSpecialCharacters)):
    print "\nValid password"else:
    print "\nPassword not valid"

Post a Comment for "Match Password With Python Regex"