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

Hmm, no .groups attitude. ‎(ノಥ益ಥ)ノ ┻━┻

I have been trying to solve this challenge for quite a while but can't seem to find a solution. Can I please get some help before I start flipping people instead of tables?

import re

string = 'Perotto, Pier Giorgio'

names = re.match(r'''([-\w]+)
(,\s[-\w ]+)''', string)

3 Answers

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

You use triple quotes and a newline, but didn't use re.VERBOSE in the flag. Which means that your pattern is looking for a literal newline in the string...which doesn't have a newline.

You'll also want to not include the comma and space in either group. It's not part of the valuable data.

Your regular expression is matching the comma and the space along with the last name. Move those to between the two groups and you should be fine.

names = re.match(r'([\w]+), ([\w\s]+)', string)

Also a nice way I've found to test python regular expressions is python-regex.com. There are some other sites that aren't language-specific out there too that even show highlighting within the content.