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

John Schut
John Schut
2,317 Points

players.py

I don't get the order of the lastname and firstname right for "Stewart Pinchback" and "Pinckney Benton". I have no idea how to do this (because there 'format' is so different from the first 4 names).

players.py
import re

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

players = re.search(r'''
                    (?P<last_name>[\w]+),?\s?:?
                    (?P<first_name>[\w]+):?\s?
                    (?P<score>[\d]+)?
                    ''', string, re.MULTILINE|re.I|re.X)

2 Answers

Steven Parker
Steven Parker
231,269 Points

You are sooooo close!

You just need to include a space with your word character classes ("[\w ]") to allow the multi-word names to be accepted as a single name.


And while it's beyond what's necessary to pass the challenge, I'd probably not allow the separator characters or the score to be optional, and make sure the names start and end with a non-space word character

players = re.search(r'''
                    (?P<last_name>\w([\w ]*\w)?),\s*
                    (?P<first_name>\w([\w ]*\w)?):\s*
                    (?P<score>[\d]+)
                    ''', string, re.MULTILINE|re.I|re.X)
John Schut
John Schut
2,317 Points

Hi Steven, I took a short break, therefor I answer this late. Thanks for your response! I am going to try it out now.