Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialTobias Edwards
14,458 PointsPYTHON: Introduction to Regular Expressions help
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 have attached my code which explains the rest of my situation through the commenting
My Question: Why doesn't this work?
Any help would be much appreciated. Thank you.
import re
# EXAMPLE:
# >>> find_words(4, "dog, cat, baby, balloon, me")
# ['baby', 'balloon']
# WHY WOULDN'T THIS WORK????
# I create a function find_words that takes a count and string as arguments
def find_words(count, string):
# Then I TRY to return a list of all the words in 'string' that is made
# up of unicode letters that are at least count word characters long.
return re.findall(r"\w{count,},", string)
# OUTPUT: Didn't get the right output. Output was []. Length was 6.
4 Answers
Kenneth Love
Treehouse Guest TeacherBecause {count}
in your regular expression isn' the same as, say {5}
in a regular expression. The 'count'
isn't going to be interpreted as a variable and replaced. In fact, it's just going to make an invalid expression, since 'count'
isn't an acceptable argument for the regular expression counts operator.
You'll need to create your pattern so that the value of the count
variable gets inserted into the pattern string. I'd suggest string concatenation.
Tobias Edwards
14,458 PointsI was just about to give up after not knowing what you meant, then it finally clicked XD Thank you!
Kenneth Love
Treehouse Guest TeacherGlad you powered through! Good work
Fredrik August Madsen-Malmo
16,261 PointsYou could also use .replace() on the string :) Personally I think that is a lot easier.
def find_words(count, string):
return list(re.findall(r'\w{count,}'.replace('count', str(count)), string))
Kenneth Love
Treehouse Guest TeacherYou don't need to turn re.findall()
into a list, it already is one.
And, yes, str.replace()
is another way of approaching it but a) we haven't covered it; and b) I'm not sure it's really easier than concatenation.
Fredrik August Madsen-Malmo
16,261 PointsAh, okay. Guess you learn something new everyday :) And I believe that Python Basics is a prerequisite for the RegEx medallion, and that covers .replace()
Kenneth Love
Treehouse Guest TeacherUh, no, Python Basics definitely doesn't cover str.replace()
. Here's my checking the script for it:
Fredrik August Madsen-Malmo
16,261 PointsAh, okay. Guess I got it mixed up with something.. Really appreciate that you took the time to check the manuscript for me! Keep up the good work :)
Kenneth Love
Treehouse Guest TeacherWanna make a content person feel like they're going crazy? Tell 'em they talked about something they didn't :D
Fredrik August Madsen-Malmo
16,261 PointsCan confirm :)
MUZ140804 Micheal Matiashe
2,779 Pointsthis is what I did but not yet working, any help, I'm still trying to solve it
def find_words(count, string): return re.findall(r'\w{'+ str(count) +'}\w?', string)
MUZ140804 Micheal Matiashe
2,779 PointsGot it now, I did it, the last \w? i added there is not needed and only a "," could solve it
Christos Peramatzis
16,428 PointsChristos Peramatzis
16,428 PointsKenneth what exactly do you mean by string concatenation? I thought string concatenation is if you do lets say: string1 = "Hello" string2 = "World" string1+string2 => "Hello World"