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

Create a variable named players that is an re.search() or re.match()to capture three groups: last_name, first_name, and

Tell me what I am doing wrong.

Thanks,

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<name>(?P<last>[\w ]+, \s(?P<first>[\w ]+)
    :\s
    (?P<score>[\d+]
    ''', string, re.X|re.MULTILINE)

2 Answers

Steven Parker
Steven Parker
231,236 Points

I see a few issues:

  • there's a stray unmatched open parenthesis before "re.search"
  • there's an unneeded and incomplete (no closing parenthesis) group for "name"
  • the group for "last" should be for "last_name" instead
  • the group for "first" should be for "first_name" instead
  • the groups for "last" and "score" are both missing the closing parenthesis
  • the regex for "score" captures only a single digit or plus-sign ("[\d+]")
  • you may have intended to leave off the character class brackets ("\d+")

I did the changes but it is only pulling Kenneth Love data.

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

Steven Parker
Steven Parker
231,236 Points

Now it looks like the "last_name" group includes the "first_name" group. The closing parenthesis should be moved so each group is separate.

Also, while it doesn't hurt, the score digits don't need to be a separate unnamed group.

I did the changes but it is only pulling Kenneth Love data.

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