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 Group Regex Challenge is not working

I keep getting "Bummer! Didn't get enough groups back!"

Unfortunately, the string is only one name and my code works on it in an IDE.

I though the first name might be optional, so I inserted an asterisk and a question mark in the following location: re.match(r'[-\w],\s[-\w ]?', string, re.IGNORECASE)

I also noticed that the text file we worked on had tabs between word groups while using spaces between the first name and the last name. However, Python coverts tabs into a spaces. In the string in this example, I can't say for certain how the next name will be entered, whether it will be separated by a space or a comma.

Thanks for the help in advance.

names.py
import re

string = 'Perotto, Pier Giorgio'
names = re.match(r'[-\w ]*,\s[-\w ]*', string, re.IGNORECASE)
MICHAEL P
MICHAEL P
5,191 Points

Your syntax is slightly off for your code.

If you want to use set brackets , you can use:

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

This is equivalent to

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

1 Answer

MICHAEL P
MICHAEL P
5,191 Points

Your syntax is slightly off for your code.

If you want to use set brackets , you can use:

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

This is equivalent to

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