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 trialJ Donahue
30,790 PointsCreate a variable names that is an re.match() against string. The pattern should provide two groups, one for a last name
import re
string = 'Perotto, Pier Giorgio'
names = re.match(r'''
^([-\w ]+,\s)
([-\w ]+)$
''', string, re.X|re.M)
I keep getting the message: " Last name was "Perotto, ", first name was "Pier Giorgio"." and I can't figure out why. I've tried adding the ?P<last> and ?P<first> to the beginning of each group, but that doesn't seem to make a difference. I've tried it with or without the string delimiters (^$). It makes no difference. I've tried switching the order of the groups, I still get the same results. I'm not sure what's going on here.
3 Answers
Megan Amendola
Treehouse TeacherYou still need to account for the comma and space in your pattern, but they shouldn't be a part of your last name group. My bad on clarity there :) You just add them outside the group.
You also need the names of your groups to create the groups()
attribute it is looking for. It's shown at about 7:45 in the video. lastname
for the first and firstname
for the second will work.
Megan Amendola
Treehouse TeacherHi, Zerksies! I believe it's because you're grabbing the comma (,) as a part of the last name. This isn't a part of the person's last name, just a grammatical requirement to have a comma when writing out someone's name in this way.
J Donahue
30,790 PointsWhen I remove the comma I get a different error.
import re
string = 'Perotto, Pier Giorgio'
names = re.match(r'''
^([-\w ]+\s)
([-\w ]+)$
''', string, re.X|re.M)
Bummer: Hmm, no .groups() attribute. Did you do a match?