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

Aidan Taylor-Lynch
Aidan Taylor-Lynch
7,630 Points

How do I match two different groups, such that the first \w+ group is different to the second \+ group

How do I match two different groups, such that the first \w+ group is different to the second + group

string = 'Perotto, Pier Georgio'

names = re.match(r'(?P<last_name>[\w]+)(?P<first_name>[\w]+ [\w]+)', string, re.I)

names.py
import re

string = 'Perotto, Pier Giorgio'

1 Answer

Peter Lawless
Peter Lawless
24,404 Points

You are SO close, Aidan. The <last_name> and <first_name> fields are separated by a comma and a space, but the expression that you provided expects the fields to be right up against each other. These tiny little details are often what make or break the match. Let me know if this fixes your problem.