Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Regular Expressions in Python Introduction to Regular Expressions Word Length

word length challenge

im stuck help me out

import re

def find_words(count, string):
    return re.findall(r'\w', string)

# >>> find_words(4, "dog, cat, baby, balloon, me")
# ['baby', 'balloon']

4 Answers

Hey! this took me about 20 friggin minutes of head-banging to figure out:

def find_words(c, string): return re.findall(r'\w+' * c, string)

I've seen a LOT of answers that involve crazy amounts of curly braces, which we haven't learned yet in this course. helpful?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,154 Points

Regex patterns provide many ways to get the correct result. I had not seen your solution to this code challenge before. Nicely done!

Your pattern may be a bit confusing to some as reads as “one or more word characters followed by one or more word characters followed by one or more word characters“. This still works due to pattern backtracking but it is less efficient.

A better presentation might be

re.findall(r'\w'*c + '+', string)

This pattern reads as “one word character followed by one word character followed by one or more word characters“.

Edit: replaced 3 with c

Chris Freeman
Chris Freeman
Treehouse Moderator 68,154 Points

You are close. What you have now finds all of the strings that contain a single word-character.

You need to modify the matching pattern to find count number of \w characters or more. For example, to find 3 or more characters you could use.

 re.findall(r'\w{3,}', string)

How would find "count or more" characters?

Post back if you need more help. Good Luck!!

still can't get it, here is the question Create a function named find_words that takes a count and a string. Return a list of all of the words in the string that are count word characters long or longer.

I'm confused by the *3 on this. We need to be able to search for the length supplied to the function. Would *c work as well?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,154 Points

Sorry, fixed example code replacing 3 with variable c