Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
210,423 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
210,423 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)