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

Didn't get the right content in the groups. Last name was "Perotto, ", first name was "Pier Giorgio".

It looks to me like I got the correct result. What am I not seeing?

names.py
import re

string = 'Perotto, Pier Giorgio'

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

Jackson, yours and mine look the same to me. Don't I have the \s outside [], for last name?

1 Answer

Jackson Lai
Jackson Lai
3,538 Points
import re

string = 'Perotto, Pier Giorgio'

names = re.match(r"""
    ^(?P<last>[\w]*,\s)     #<-- in here '',\s" should be out of the bracket
    (?P<first>[\w\s]*)$
""", string, re.X|re.M) 
Jackson Lai
Jackson Lai
3,538 Points

I didn't give you the answer on my last comment, I just told you what you should do. Here is my answer.

import re

string = 'Perotto, Pier Giorgio'

names = re.match(r"""
    ^(?P<last>[\w]*),\s     #<-- in here ",\s" should be out of ()
    (?P<first>[\w\s]*)$
""", string, re.X|re.M)