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 Name Groups

names.py /groups.() attribute not used ? /

Alright I am have a difficult time understanding this challenge. I am getting a error message "Hmm Hmm, no .groups() attribute. Did you do a match?

names.py
import re

string = 'Perotto, Pier Giorgio'

names = re.match("""
    ^(?P<first>,\s\b[-\w]*)
    (?P<last>,\s[-\w]*\b)
""",string,re.X|re.M)

2 Answers

Christian Mangeng
Christian Mangeng
15,970 Points

You are getting closer now.

You now need to exclude the comma and space between last and first name from the groups, so put these between the groups like that (?P<last>your_code),\s(?P<first>your_code).

Completed the exercise with (?P<last>[\w]+),\s(?P<first>[\w]+\s[\w]+). Thank you for your help.

Christian Mangeng
Christian Mangeng
15,970 Points

Hi Alexander,

I think it's best you review the videos and check the terms, as your version is quite far from the solution. Some suggestions:

1) Don't forget to add the r at the beginning of the opening parenthesis

2) Use [-\w]+ instead of [-\w]*, because you can see from "string" that the word characters must exist and therefore are not optional

3) The first name consists of two words, so you will need to use [-\w]+ or [\w]+ twice in that group

4) You don't really need word boundaries (\b) here

Hope that helps, Good luck!

It kinda helps. I rewatched the previous lessons and I still have no idea what am doing. Still getting the same error if I remove a comma from (?P<last>[-\w]+,) Hmm, no .groups() attribute.

If I do not then I get the this error message: Didn't get the right content in the groups. Last name was "Perotto,", first name was " Pier Giorgio".

Also "The name parts are separated by a comma and a space." This sentence in the instructions has me rather confused. Im not sure what it is referring to.

names = re.match(r""" (?P<last>[\w]+,) (?P<first>\s[\w]+\s[\w]+) """, string, re.X)