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

Please help!

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.

I get this error:

Bummer: error: nothing to repeat at position 73 (line 4, column 14)

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

not sure what to do from here...

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,425 Points

Hey diginoma, Often the error message has all the info you need. The 73rd character of your regex is the ? following <score>. This means "one or none" of something. But there isn't anything between the field name and the ?. Hence the error. Remove this ?, and you're on your way....

The code will run but return a wrong value. Hint: are you capturing spaces that might be within the first or last names?

Your re flags are correct.

  • re.X is for verbose code making all whitespace explicitly encoded in the regex (regular whitespace is ignored).
  • re.M is for multiline, in that, you wish to run the regex pattern on each line (up to the newline char) within the multi-line input string.

Post back if you need more help. Good luck!!