Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Regular Expressions in Python Introduction to Regular Expressions Email Groups

Stuck on Code Challenge Regular Expressions

Not sure where to go from here... getting "Bummer! error unexpected end of pattern"

help!?

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)

3 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Couple of things.

  1. What's between the email address and the phone number in the string? Is that referenced in your pattern? It should be, but shouldn't be in either group.
  2. You're doing a verbose pattern but you aren't using the re.VERBOSE flag, so your pattern is looking for a set of spaces before the groups and a newline between them.

I'm also having problems with this exercise. What's wrong with this code?

contacts = re.search(r'''
    ^(?p<email>[-\w\d.+]+@[-\w\d.]+),\s
    (?P<phone>\d{3}-\d{3}-\d{4})$
''', string, re.X|re.M)
Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Since our email addresses and phone numbers aren't at the front or end of a line/string, you don't want to use ^ and $.

Hmm, I still can't get it to work after taking that out

contacts = re.search(r'''
    (?p<email>[-\w\d.+]+@[-\w\d.]+),\s
    (?P<phone>\d{3}-\d{3}-\d{4})
''', string, re.X|re.M)
Hunter MacDermut
Hunter MacDermut
15,865 Points

Patrick, your email group needs a capital letter P instead of a lowercase.

I'm having issues with this as well. Here's my regex:

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'''

line = re.search(r'^(?P<last_name>\w+), (?P<first_name>\w+), (?P<email>[-\w\d.+]+@[-\w\d.]+), (?P<phone>\d{3}-\d{3}-\d{4}), (?P<twitter>@[\w\d]+)', string, re.M)

line.groupdict()

When I run that code locally, using Python 3.4.2, I get:

{'last_name': 'Love', 'first_name': 'Kenneth', 'email': 'kenneth+challenge@teamtreehouse.com', 'twitter': '@kennethlove', 'phone': '555-555-5555'}

So the fields are there, and appear to be working, but when I run the Code Challenge, all I get is:

Bummer! Didn't get the right groups from your regex.

I've tried a dozen variations on this, with no luck. Can anyone spot the issue?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

It's just supposed to get the email address and phone number from each line. You're getting names and twitter handles, too.

Ah, that's what I get for trying to overdo it. Thanks!