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 trial

Python Regular Expressions in Python Introduction to Regular Expressions Email Groups

Haisam Elkewidy
Haisam Elkewidy
26,987 Points

patterns seem right, says it didnt get a regex search object

Not sure what I'm doing wrong here..

emails.py
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'''
^(?P<name>[-\w]*,\s[-\w ]+)\t,
(?P<phone>\(?\d{3}\)?-?\s?\d{3}-\d{4})?\t
''', string)

1 Answer

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Haisam,

You're pretty close, but you have a few minor mistakes in your pattern. Let's step through your pattern. But first, I'm going to suggest my favourite website for testing regexes: regex101.com.

I suggest you paste your string into the Test String box and start typing out your pattern in the Regular Expression box.

You should be able to type the following (from your code) and find you are matching on the name, just as you would expect:

^(?P<name>[-\w]*,\s[-\w ]+)

Then we get to the first problematic token: \t. As you may recall (and regex101 will remind you), this will only match a tab character. In the case of the test string, there is no tab between the end of the characters comprising the name and the comma.

Assuming we correct the tab, the match continues until we reach our next, subtle, problematic token: the newline character. Because you've broken your string over multiple lines, the regex engine treats the move to the next line as a literal newline character.

Assuming we fix the newline, we now need to get to the beginning of our phone group. You might want to try something that matches any character, one or more times.

Now let's try adding your phone group, so far, so good.

Lastly, let's look at the tokens you have following your phone group. You have a ?, this causes your group to match zero or more times, so it matches zero times and all the characters that would have been in your phone group get caught instead by the previous "match any character" sequence. We don't need this quantifier (every record in your test string has a phone number) so let's just get rid of it. Then you have another tab capture character \t. There are no tabs in the test string, so this causes your whole regex to not match anything. Moreover, we don't actually care about anything after the phone group for any particular match, so again, let's just get rid of it.

Now we'll see in regex101 that our regex matches each name and phone number in our test string, so now we can head back over to the challenge page and paste it back into your raw string.

Wait, now we get a message saying "Bummer: didn't get the right groups from your regex." This is because we didn't read the challenge carefully. It's asking for a group called 'email' and a group called 'phone', but we've given it a group called 'name' and a group called 'phone'.

Based on the fixes we've walked through above, I'm sure you can tweak your regex to capture the email instead of the name, but if you have trouble, let me know and we'll walk through your regex again.

Cheers

Alex