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

benjamin siverly
benjamin siverly
3,430 Points

"Contacts" not returning regex search object... Wondering how to fix email and phone number search...

I've been fiddling around for a few hours on this challenge. It's the first time I've had any problems with the regex search/match/findall issues.

Now it is saying I don't even have a regex search object, and I'm not sure a) what that means b) why it has taken me so long to get this process in order.... Is this somehow more complicated than it seems?

Kenneth Love

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)

1 Answer

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Three things I noticed about your pattern.

  1. You have [ brackets around the different parts of the phone number. Generally those turn the bits inside into literal values, so it would be looking for actual {s and 3s and such. I'm not positive that that applies to escape sequences and counts but I wouldn't take that chance.
  2. You aren't including the commas and spaces in your pattern. They should be there because they're there in the string. Just make sure they're not part of your groups.
  3. And, lastly, you're using a multi-line pattern but not marking it as multi-line with the re.X or re.VERBOSE flag. That'll cause Python to interpret your newlines as actual newlines, which aren't in that part of the string.

Once I fixed these bits, I was able to pass the step with your code.

You got this!

benjamin siverly
benjamin siverly
3,430 Points

Kenneth Love

Got it to work! Thanks! For some reason I thought I only needed to mark re.X if I included comments outside of the groups... So that makes much more sense now!