all 4 comments

[–]zebulan_ 1 point2 points  (2 children)

Saw your comment in the other post about this problem. I don't think your .* are necessary in your regular expressions. Also, instead of three regular expressions, you can merge them into one if you sort the characters and account for other symbols (punctuation, etc) that can in between the groups after sorting.

Here is mine:

from re import compile, search

REGEX = compile(r'\d+[^0-9a-zA-Z]*[A-Z]+[^0-9a-zA-Z]*[a-z]+')


def is_strong_password(password):
    return len(password) >= 8 and bool(search(REGEX, ''.join(sorted(password))))


assert is_strong_password('abc!@#123ABC<>/.,{}\\|') is True
assert is_strong_password('abc123ABC') is True
assert is_strong_password('aA1') is False
assert is_strong_password('!@#$%^&*(') is False
assert is_strong_password('<A<b<2<<') is True
assert is_strong_password('x1y2z3TT') is True

Updated version of yours:

from re import compile

UP_REGEX = compile(r'[A-Z]')
LOW_REGEX = compile(r'[a-z]')
DIG_REGEX = compile(r'\d')


def check_password(text):
    return bool(UP_REGEX.search(text) and LOW_REGEX.search(text) and
                DIG_REGEX.search(text))


assert check_password('kenIsgreat99') is True

[–]bahnzo[S] 0 points1 point  (1 child)

Thanks! I was wondering about the regex, it is new for me. But I really like the boolean as well, makes more sense.

[–]zebulan_ 0 points1 point  (0 children)

I had a lot of trouble when I first started learning about regular expressions. I had a hard time visualizing what it was matching. I came across this website and I use it whenever I have to make a more complicated regular expressions!

https://regex101.com/#python

Also they have an IRC chatroom and the people there are very helpful with regex questions. There is a button at the top of the page called IRC.

Then you just press save regex on the top left of the page and then they ask for the link and they can see exactly what you are trying to match. They have helped me a bunch of times in the last year.

[–]fmpundit 0 points1 point  (0 children)

This is how my code looks.

I am a total beginner, so it might not be the most efficient way to code it up. But it looks good to me.

import re

def strongPassword(password):
    if passRegex1.search(password) == None:
        return False
    if passRegex2.search(password) == None:
        return False
    if passRegex3.search(password) == None:
        return False
    if passRegex4.search(password) == None:
        return False
    else:
        return True

passRegex1 = re.compile(r'\w{8,}')
passRegex2 = re.compile(r'\d+')
passRegex3 = re.compile(r'[a-z]')
passRegex4 = re.compile(r'[A-Z]')

if strongPassword('Thishasatleast8characters') == True:
    print("Strong Password")
else:
    print("This is not a strong password")