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 Players Dictionary and Class

I don't understand what output is expected. I did define groups and when running it in my own editor it returns the data

What is th expected output?

players.py
import re

string = '''Love, Kenneth: 20
Chalkley, Andrew: 25
McFarland, Dave: 10
Kesten, Joy: 22
Stewart Pinchback, Pinckney Benton: 18'''


pattern = r'''
(?P<last_name>[\w]+),?\s?
(?P<first_name>\s[\w]+):\s
(?P<score>[\d]+)
'''

players = re.match(pattern, string, re.X|re.M )

1 Answer

Steven Parker
Steven Parker
231,236 Points

It's easy to miss, but look very carefully at the error message:
:point_right: Bummer: Didn't get the expected match. Got "{'last_name': 'Love', 'first_name': ' Kenneth', 'score': '20'}".

You can see that ' Kenneth' has a space inside the quotes, right at the front. The regex should handle a space between the names, but not capture one as part of a name.

Once that's fixed, you'll also need a few tweaks to mark the line boundaries and to accommodate special cases such as the multiple-word names in the last example.