Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,116 Points

Hope below pattern will help

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

1 Answer

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?