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

Devin Scheu
Devin Scheu
66,191 Points

Python Help

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

Code:

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 }+))\t   #  last and first names
  (?P<score>\(?\d{2}\)?)     # score
''', re.MULTILINE)

print(players)

1 Answer

Hi Devin,

That's a really good attempt. Let's try and build it incrementally and see if that helps.

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

Regular expression functions have this basic syntax:

re.search(pattern, string[, options])

With this, we can build our skeleton function:

players = re.search(r'', string, re.M)

We have to capture 3 specific groups: last_name, first_name, and score, so let's set those up next.

players = re.search(r'(?P<last_name>)(?P<first_name>)(?P<score>)', string, re.M)

Finally, we need to figure out how to pattern match each of those groups. One thing to keep in mind is that the challenge is giving us a player with - what amounts to be - two last names and two first names. As ridiculous as that is to say, that's a practical way of thinking about it when writing the pattern.

For last names, one or more word characters, followed by an optional space, followed by an optional sequence of one or more word characters. In code, that looks something like this:

players = re.search(r'(?P<last_name>[\w]+\s?[\w]+?) # ...

I'll leave the rest for you to finish. I hope this was a helpful nudge in the right direction!

Cheers

Devin Scheu
Devin Scheu
66,191 Points

Okay so this is what I got, i'm getting an error: unexpected end of regular expression

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 }+))\t(?P<score>\(?\d{2}\)?)', string, re.M)
Devin Scheu
Devin Scheu
66,191 Points

EDIT: I didn't read your statement all the way through, now i'm getting error: players doesn't seem to be an rgex object

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>[\w]+\s?[\w]+?)(?P<score>\(?\d{2}\)?)', string, re.M)

Python doesn't know when one group ends and another begins.

# For example,
# (?P<last_name>[\w]+)(?P<first_name>[\w]+)

# it is ambiguous as to where the last name ends and the first name begins

Between these groups, we need to include any characters that will match but will not be selected. After the last name, we can expect to see a comma followed by a space.

players = re.search(r'(?P<last_name>[\w]+\s?[\w]+?),\s(?P<first_name> # ...

After the first name, we can expect to find a colon followed by a space. And finally, for the score, I'd try to match one or more digits. There are no parentheses in the string provided, so I wouldn't try to optionally match those.

To help make the pattern easier to read, we can break it up onto multiple lines if we pass the verbose flag re.VERBOSE or re.X.

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

Thanks Robert! I understand now.