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

Groups, get error on - (dash) in phone number

Below is my code. I think it is basically the same as the video.

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.]+)\t ##email address (?P<phone>(?\d{3})?\s?-?\d{3}-\d{4})?\t ##phone number ''',string, re.X|re.M)

I get a no regex search object. If I add in square brackets for the phone number (not sure why these are left out for the phone number in the video) that line becomes:

    (?P<phone>[\(?\d{3}\)?\s?-?\d{3}-\d{4}])?\t    ##phone number

I then get a bad character error }-\ (indicating the - (dash) between \d{3} and \d{4} is causing an error. It does not in the video.

Please help. I am finding this whole module so frustrating.

1 Answer

Steven Parker
Steven Parker
231,269 Points

I see a few issues (in the original code):

  • there's a stray "?" at the beginning of the phone group
  • the entire phone group should not be optional
  • the area code portion of the phone number should not be optional
  • the groups aren't followed by tabs (\t)
  • the comma and space between groups must be accounted for (but not part of either group)

And when posting code, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.