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 trialJamison Imhoff
12,460 PointsStuck on Code Challenge Regular Expressions
Not sure where to go from here... getting "Bummer! error unexpected end of pattern"
help!?
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
Treehouse Guest TeacherCouple of things.
- 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.
- 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.
Patrick Farnach
1,351 PointsI'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
Treehouse Guest TeacherSince our email addresses and phone numbers aren't at the front or end of a line/string, you don't want to use ^
and $
.
Patrick Farnach
1,351 PointsHmm, 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
15,865 PointsPatrick, your email group needs a capital letter P instead of a lowercase.
Rob Sears
9,247 PointsI'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
Treehouse Guest TeacherIt's just supposed to get the email address and phone number from each line. You're getting names and twitter handles, too.
Rob Sears
9,247 PointsAh, that's what I get for trying to overdo it. Thanks!
Jamison Imhoff
12,460 PointsJamison Imhoff
12,460 PointsKenneth Love