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

"Bummer! `players` doesn't seem to be a regex object". Hunh?

What's wrong with this code? Is it related to something with the regex string or is it related to my function call? Please explain :)

Any help appreciated.

Thanks, Alex

players.py
import re

string = '''Love, Kenneth: 20
Chalkley, Andrew: 25
McFarland, Dave: 10
Kesten, Joy: 22
Stewart Pinchback, Pinckney Benton: 18'''

players = re.match(r'''
    (?P<last_name>[a-zA-Z]+),\s
    (?P<first_name>[a-z-A-Z]):\s
    (?P<score>\d+)
''', string, re.MULTILINE)

Tagging Chris Freeman

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

When the match isn't found, a None is returned which results in the error seen.

There are some errors in the regex:

  • to use the plus sign signifying "one or more", you need to wrap the set of characters in parens. Do this for both the first and last name sets.
  • since one of the first and last names includes a space, add a \s in the character set
  • add the switch re.VERBOSE to ignore the extra white space in the regex. Remember to include a pipe symbol between the re switches.
  • include a start-of-line anchor ^ at the start of the pattern to align properly for the multiline matching

It is still producing the same error, sadly.

Here's my code:

import re

string = '''Love, Kenneth: 20
Chalkley, Andrew: 25
McFarland, Dave: 10
Kesten, Joy: 22
Stewart Pinchback, Pinckney Benton: 18'''

players = re.match(r'''
    (?P<last_name^(a-zA-Z)+),\s
    (?P<first_name>^(a-z-A-Z)+):\s
    (?P<score>^\d+)
''', string, re.MULTILINE|re.VERBOSE)
Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

Sorry. I was misleading about the parens and the plus sign. You can use the plus sign also on a square bracket character set as in you original code for last_name. A plus is also needed in the same manner on the first_name.

  • Still need a \s added into the first_name and last_name character sets.
  • when matching on a hyphen it is usually best to put it as the first character in the set so it isn't confused with a character range.
  • only a single caret ^ is needed in front of the first group paren.

Here is one solution:

players = re.match(r'''
    ^(?P<last_name>[a-z\sA-Z]+),\s
    (?P<first_name>[-a-z\sA-Z]+):\s
    (?P<score>\d+)
''', string, re.MULTILINE|re.VERBOSE)

Thank you! I understand now.

However, I'm still confused on the ^ and why you need it... What's it for?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

The caret ^ matches the beginning of the line or each line start in re.MULILINE. More at docs

Thanks!