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

David Bell
David Bell
16,997 Points

Group Email Challenge Task 1: Multiple lines not creating a regex object

I've tried this a few ways and I've debugged down to the code below, which is not creating a regex search object.

What I've found is that either group, by itself, does return some regex object (or at least is recognized as not having both groups). But, with both included, the regex object is not being created. Feeling stumped, but I want to get on the right direction again.

Note: I didn't include the Multiline flag and components, but this seemed permissible since I'm not searching for all the parts of the string: just these 2 email and phone pieces. Please correct me if that's wrong!

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<email>[-\w\d.+]+@[-\w\d.]+)
(?P<phone>\d{3}-\d{3}-\d{4})
""", string, re.VERBOSE)

2 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

You left out what comes between the email and the phone number. Remember, once the pattern starts matching, it has to be correct until it stops (that was why we didn't match some of the lines in the video when we were just starting)

David Bell
David Bell
16,997 Points

Aaaah, I see! So, for example, if I wanted to grab just name and number from that challenge string in one regex expression, I'd have to describe the email pattern in between to get there.

Thanks!!

Hara Gopal K
PLUS
Hara Gopal K
Courses Plus Student 10,027 Points

thank you. one yr later, i came across the same problem...:)