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

Aldo Rivadeneira
Aldo Rivadeneira
3,241 Points

Regex challenge failed: Bummer: Didn't get the right content in the groups. Last

I was testing the code in several ways to find the correct answer but i don't know what's the error. ¯_(ツ)_/¯

names.py
import re

string = 'Perotto, Pier Giorgio'
names = re.match(r'''
                 (?P<firstname>[-\w]+),(?P<Lastname>\s[-\w\s]+)
                ''', string, re.X)
Saikat Chowdhury
Saikat Chowdhury
3,128 Points

Hope below pattern will help

names = (re.match(r''' (?P<firstname>[\w]+ ),\s (?P<lastname>[\w ]+) ''',string,re.X))

1 Answer

csr13
csr13
33,290 Points

Try and catch the \s whitespace before the second group and before the comma. Maybe use * instead of + . Also try using the ^ at the start and the $ at the end.

Im prolly going to get bashed for doing this, but, look, regexes are just one of those confusing things that are tricky..

Maybe something like this:

names = re.match(r'^(?P<firstname>[\w]asterisk),\s(?P<Lastname>[\w\s\w]asterisk)$', string, re.X)

Catch my drift?