Euler
Euler

Reputation: 355

Match password with Python regex

The requirements for my password are:

So I want to use the re module to check my string input: re.search(pattern, string).

What should be my pattern here?

Upvotes: 0

Views: 5888

Answers (3)

Ben Holland
Ben Holland

Reputation: 2309

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"

Upvotes: -4

Avinash Raj
Avinash Raj

Reputation: 174706

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.

Upvotes: 11

GLHF
GLHF

Reputation: 4035

while True:
    passw = input("password: ")
    if len(passw)<10:
        print ("not enough")
        continue
    if passw.isalpha():
        print ("atleast 1 digit")
        continue
    if passw.islower():
        print ("at least 1 uppercase")
        continue
    if pass.isupper():
        print ("Do you even password?")
        continue
    else:
        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.

Upvotes: 2

Related Questions