0

I am new to coding and python is my first programming language. I am studying with 'Automate the Boring Stuff', and right now I am in CH7- trying to figure out 'Strong Password Detection' project.

(https://automatetheboringstuff.com/2e/chapter7/)

Strong Password Detection

Write a function that uses regular expressions to make sure the password string it is passed is strong. A strong password is defined as one that is at least eight characters long, contains both uppercase and lowercase characters, and has at least one digit. You may need to test the string against multiple regex patterns to validate its strength.

I wrote a code like this below.

import re, pyperclip

password_regex = re.compile(r'^(?=.*[a-zA-Z0-9])(?=.*[!@#$%^&*-_]){8,}$')

text = pyperclip.paste()

matches = []

for groups in password_regex.findall(text):
    matches.append(groups[0])
    if len(matches)> 8:
        pyperclip.copy('\n'.join(matches))
        print('\n'.join(matches))

    else:
        print('Your password lenths doesn\'t meet the standard.')

It doesn't run anything after I saved it. Would you please help me and teach me what I did wrong? Thank you so much in advance.

maasan20
  • 1
  • 1
  • You are repeating the negative lookahead assertion `{8,}` which is non consuming. You could repeat for example a dot to match any char except a newline or be more specific what to repeat `^(?=.*[a-zA-Z0-9])(?=.*[!@#$%^&*-_]).{8,}$` – The fourth bird May 07 '20 at 16:44
  • Thank you for the comment. I tried it again, but still it doesn't work. Would you please show me if you can write the regex code from the beginning? Thanks! – maasan20 May 07 '20 at 18:20
  • You have a wrong regex. It insists that a password should look like 'a@a@a@...' - at least 8 interleaving chars from two segments: [a-zA-Z0-9] and [!@....]. – Alexey Ruzin May 08 '20 at 19:02
  • You have a wrong regex. It insists that a password should look like 'a@a@a@...' - at least 8 interleaving chars from two segments: [a-zA-Z0-9] and [!@....]. I recommend 1) check https://regex101.com/ for fast evaluation of your regexes 2) read more about regexes (a book or at least an article) in your case regex should look like: ```^(?=.*[a=z])(?=.*[A-Z])(?=.*[0-9]).{8,}$``` - check for small letter, check for caps letter, check for digit, then take it if it has at least 8 symbols – Alexey Ruzin May 08 '20 at 19:10

0 Answers0