Learn Regular Expressions by Building a Password Generator - Step 49

Tell us what’s happening:

It says to turn pattern into the shorthand class for non-alphanumeric characters, which is ‘\W’. This is what I am doing. When it showed as wrong, it says that my pattern variable should be ‘\W’, which is what it is, but it is still saying it is wrong.

Your code so far

import re
import secrets
import string


def generate_password(length, nums, special_chars, uppercase, lowercase):
    # Define the possible characters for the password
    letters = string.ascii_letters
    digits = string.digits
    symbols = string.punctuation

    # Combine all characters
    all_characters = letters + digits + symbols

    while True:
        password = ''
        # Generate password
        for _ in range(length):
            password += secrets.choice(all_characters)
        
        constraints = [
            (nums, r'\d'),
            (lowercase, r'[a-z]'),
            (uppercase, r'[A-Z]'),
            (special_chars, r'\W')
        ]        

    return password
    
# new_password = generate_password(8)
# print(new_password)

# User Editable Region

pattern = '\W'
quote = 'Not all those who wander are lost.'
print(re.findall(pattern, quote))

# User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Safari/605.1.15

Challenge Information:

Learn Regular Expressions by Building a Password Generator - Step 49

You should still have a raw string. You removed the r from the existing pattern.

1 Like