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

Why is 'player' not recognized as 'regex' object?

Code challenge for Compiling and Loops; code -

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   # Last Names
    (?P<first_name>[-\w]+):\s   # First Names
    (?P<score>[\d]+)   # Score
''', string, re.X|re.M).group()   # VERBOSE and MULTILINE
  • works in WorkSpace but gets " 'players` doesn't seem to be a regex object." in challenge. I'm probably missing some nuance they are looking for in this case.?

3 Answers

This one really stumped me for a while. And then I just got it. was getting the same 'player doesn't seem to be a reggae object' comment.

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

ha regex not reggae

James N
James N
17,864 Points

thanks! this helped me out, too!

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

You aren't allowing a person to have multiple words in each name, like our last player Pinckney Benton.

Yep, that's it. I'm not allowing for a person to have multiple words in each name, like last player Pinckney Benton. (ooooh, theys tricksey, my precious ...) Thx!

Perry

Gavin Ralston
Gavin Ralston
28,770 Points

When you .group() the search object, you're going to get a string back. So type(players) is a string

Without the .group() method, you get a search object back. Is that what is being asked for in the challenge?

gavin, Thx! for resp, appreciate the thot. Trying w/o 'group, groups, or groupdict' gets "Bummer! Didn't get the right results. Got [('Love', 'Kenneth', '20'), ('Chalkley', 'Andrew', '25'), ('McFarland', 'Dave', '10'), ('Kesten', 'Joy', '22')]." The instruction is: 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.

Gavin Ralston
Gavin Ralston
28,770 Points

So once you remove group, you're getting back all of the lines except for the last one.

How are you dealing with the last line, which looks like a pair of people with one score?

Or maybe the person is really named Pinckney Benton Stewart Pinchback.

Are you dealing with the fact that a first name and a last name can include whitespace?

edit: Knew of Robert Smalls, Frederick Douglass, etc. PBS Pinchback? How could he not come up, if not just to invoke the name?