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 trialBrent Brinkley
10,910 PointsEmail Groups exercise in Python RegEX course is broken.
I keep getting an error stating that contacts isn't receiving a regex search object. When I change the \d numeric pattern to \w character pattern the code executes fine but gives me back an error stating that it's not properly formatted due to receiving back letters, and correctly so. In my testing I can't get it to give me back anything involving the \d pattern.
import re
string = '''Love, Kenneth, kenneth+challenge@teamtreehouse.com, 555-555-5555, @kennethlove
Chalkley, Andrew, andrew@teamtreehouse.co.uk, 555-555-5556, @chalkers
McFarland, Dave, dave.mcfarland@teamtreehouse.com, 555-555-5557, @davemcfarland
Kesten, Joy, joy@teamtreehouse.com, 555-555-5558, @joykesten'''
contacts = re.search(r"""
[^,\s]+
(?P<email>[-\w\d.+]+@[-\w\d.]+)
(?P<phone>\d{3}-\d{3}-\d{4})
""", string, re.X|re.I)
1 Answer
Chris Howell
Python Web Development Techdegree Graduate 49,702 PointsHey Brent,
So I ran your code, the re.search returns None. Which means your code didn't error and doesn't meant the challenge is broken. Either the regex will return a regex match object or it will return a None if the pattern cant be matched this makes it easy for us to check if we have an actual match or not with no other data types in the mix.
I adjusted your code and got it to pass this challenge just fine. The more you play with RegEx expressions the more you will get the hang of it.
I am not a fan of just handing out the answer because you wont really truly learn it, right?
So break down each chunk of your regex and imagine how it looks at the string with each character you are asking for it to look for.
Keep in mind that using ^ is going to start its search at the beginning of the string and when using MULTILINE mode it will check each "New line" as well.
If I were going to give you hints I would say.
- Your first grouped statement may not be needed ;)
- Your email and phone patterns look fine, but to match what is in the string after the email you may have to add a few characters similar to that in the first grouped statement instead but it, itself may not even need to be a grouped statement as you are catching name values from the pattern using ?P
Let me know if this helps, you get any closer? Running this in my own Python shell on my computer helped me print the output of contacts so I could see if it held a Match obj or a None value.
Chris Howell
Python Web Development Techdegree Graduate 49,702 PointsChris Howell
Python Web Development Techdegree Graduate 49,702 PointsSide Note
RegEx is only going to CARE what you tell it to care about. Aside from a small bug in the pattern which you will soon fix im sure :)
The RegEx is looking for the email pattern and the phone pattern (whatever you listed that is). If we step-by-steped through regex's actual search, it actually may find the pattern of email during its search right then the moment we gave it something or maybe accidently forgot to account for a few characters that may be there. The pattern no longer matches and immediately return that None.
It only matches until it doesnt. Think that way when you write your regex. "Where can this not match or in what cases may this no longer match my pattern"