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

SUDHARSAN CHAKRAVARTHI
PLUS
SUDHARSAN CHAKRAVARTHI
Courses Plus Student 2,434 Points

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

I don't know where i am doing wrong... can some one help me ?. Thank you...

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*\w*]+),
                   \s(?P<first_name>[-\w\s*\w]+): 
                   (?P<score>\s[\d]+)'
                   ''', string, re.X | re.M)

7 Answers

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

This will work

So. This is what my solution looks like ,but still no luck in workspaces.

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\w]+),
    (?P<first_name>\s[\w\s*\w]+):
    (?P<score>\s[\d]+)
''', string, re.X | re.M)

But on my local machine it seem to be working fine. I have no clue. Chris Freeman if I could ask for your help please. MUCH! apricated .

<re.Match object; span=(0, 17), match='Love, Kenneth: 20'>

Same for me . in my text editor it seems to be working just fine. But I can't seem to get it to work well in the workspace

Ayman Omer
Ayman Omer
9,472 Points
players = re.search(r''' ^(?P<last_name>[-\w\s*\w*]+), \s(?P<first_name>[-\w\s*\w]+): \s(?P<score>[\d]+)$ ''',string, re.X | re.M)
players = re.search(r'''
                   (?P<last_name>[-\w\s*\w*]+),
                   \s(?P<first_name>[-\w\s*\w]+): 
                   (?P<score>\s[\d]+)' # remove this single quote
                   ''', string, re.X | re.M)

Just remove the unnecessary single quote and, remember, if you need to use one, it needs to be escaped with a "\" as it also can denote the end of a multi-line string.

SUDHARSAN CHAKRAVARTHI
SUDHARSAN CHAKRAVARTHI
Courses Plus Student 2,434 Points

Thank You. Even after removed the comment , still i am unable to get the desired result. re.search(r''' (?P<last_name>[-\w\s*\w*]+), (?P<first_name>\s[-\w\s*\w]+): (?P<score>\s[\d]+) ''', string, re.X | re.M)

players = re.search(r''' (?P<name1>[-\w\s\w]+), (?P<name2>\s[-\w\s*\w]+): (?P<name3>\s[\d]+) ''', string, re.X | re.M)

This works for me. It looks like you removed your name specifiers from the regex. When you type ?P at the beginning of the group, Python looks for a name directly after enclosed in <>. Failing to give this name after using ?P will raise an error. Just replace name1, name2, and name3 with your group names or leave out the ?P<name> tag altogether.

SUDHARSAN CHAKRAVARTHI
SUDHARSAN CHAKRAVARTHI
Courses Plus Student 2,434 Points

Thank you very much for your description. But still there is something issue in my code. i did same thing as you explained.

 players = re.search(r'''
         (?P<last_name>[-\w\s\w]+),
         (?P<first_name>\s[-\w\s*\w]+):
         (?P<score>\s[\d]+)
         ''', string, re.X | re.M)
SUDHARSAN CHAKRAVARTHI
PLUS
SUDHARSAN CHAKRAVARTHI
Courses Plus Student 2,434 Points

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

unable to trace where am i doing wrong ?.

SUDHARSAN CHAKRAVARTHI
PLUS
SUDHARSAN CHAKRAVARTHI
Courses Plus Student 2,434 Points

somebody can help me to move ahead for the below challenge:

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