Learn Regular Expressions by Building a Password Generator - Step 34

I have tried different things but unable to get right answers.
Pattern = ‘l+’[wha]
Pattern = ‘l+’ + [wha]
Pattern = ma[wha]
Pattern = ‘[wha]’
I was asked to the following:-

A character class is indicated by square brackets and matches one character among those specified between the brackets. For example, ma[dnt] matches either mad, man, or mat.

Modify your pattern to match a w followed by either h or a.

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, '')
        ]        

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

# User Editable Region

pattern = 'l+'
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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0

Challenge Information:

Learn Regular Expressions by Building a Password Generator - Step 34

You appear to have created this post without editing the template. Please edit your post to Tell us what’s happening in your own words.

here you have a pattern that matches ma followed by either a d, a n, or a t, it matches three characters. A character class matches only one character at a time, but one from a list.

You need to write

so two characters, the first is a w, the second a h or an a. The second character is matched from a list of characters so for the second character you need to use a character class.

1 Like

This Worked for me;

redacted solution

hey @dk665226

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

1 Like

I have tried but mine still don’t work!
for example, i use a character class like this : was[h] or wall[a] but still don’t work!

This is an example of the code that i have used:
pattern = ‘W[ha]mmy l+’ / w[hammy] ‘l+’
quote = ‘Not all those who wander are lost.’
print(re.findall(pattern, quote))

nothing works for the codes that i have been written!

you need to create a pattern following the instructions, so it should match w followed by either h or a (here you need the character class. So it would match wa and wh, not whammy

yes! i have tried many expression, none of them works for me.

Whammy was my last tried. so it didn’t work

try writing a pattern that matches a w, only that, then we do the second step

i got it! thank you!