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 trialAndrew Gordon
10,178 PointsName Groups Challenge 1/1
I don't understand what I'm missing in regards to this challenge. Sometimes I feel like I over complicate my answers, but I don't feel that is the issue here:
import re
string = 'Perotto, Pier Giorgio'
names = re.match(r'?P<name>(\w, \w)*', string)
Any words of wisdom?
Thanks! Andrew
1 Answer
Chris Freeman
Treehouse Moderator 68,441 PointsYour regex is not using the correct syntax for a named groups, also challenge wants two groups: firstname, lastname
import re
string = 'Perotto, Pier Giorgio'
ā
names = re.match(r'(?P<lastname>\w+), (?P<firstname>[\w ]+)', string)
Andrew Gordon
10,178 PointsAndrew Gordon
10,178 PointsAh I see. the syntax did feel off, and I also see that I missed the fact that the challenge wanted two groups, not necessarily for them to be subgroups. Appreciate the input!
jack pincombe
6,188 Pointsjack pincombe
6,188 PointsI may have missed it in the video but why are there no [] for the \w+ in the last name group but there are for the first name
(?P<lastname>\w+) vs (?P<firstname>[\w ]+)
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsThe
[ ]
define a character set. Since a match on the last name can be described with a single character pattern there is no need to use the set brackets to define a set.Set brackets can be used if you wish:
re.match(r'(?P<lastname>\w+), (?P<firstname>[\w ]+)', string)
Is equivalent to
re.match(r'(?P<lastname>[\w]+), (?P<firstname>[\w ]+)', string)