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

Doesn't seem to compile for me in the task, but is ok when I run it in the python window on my pc.?

Here's the code I am running, seems to work ok, doesn't seem to work in the task?

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

players = re.search(r"(\w+),\s(\w+):\s(\d+)", string, re.MULTILINE)

players <_sre.SRE_Match object; span=(0, 17), match='Love, Kenneth: 20'>

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"(\w+),\s(\w+):\s(\d+)", string, re.MULTILINE)

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

You are on the right track. Add names to the groups in parens, Plus add spaces to the characters allowed for the first and last name:

players = re.search(r"(?P<last_name>[\w ]+),\s(?P<first_name>[\w ]+):\s(?P<score>\d+)", string, re.MULTILINE)
Gunhoo Yoon
Gunhoo Yoon
5,027 Points

Error comes from two areas: un-handled edge case and unfulfilled challenge requirement.

  1. There seems to be an edge case for this line of data Stewart Pinchback, Pinckney Benton: 18 As you can see Stewart Pinchback does not follow the general convention of data format. I've tinkered a bit and figured out that challenge wants you to ignore not properly formatted data which is Stewart Pinchback (but not Pinckney Benton).

  2. Challenge asks you to capture them in a named group <last_name>, <first_name> and <score>. I tried without them and it failed as expected so you should include them in your pattern. I'm pretty sure you know how as you've made it this far.

If you are having trouble figuring the solution let me know, I'll post my passing code.

Thanks Guys, putting in the (tags/parens) seemed to do the trick ?P<last_name> etc...