Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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 67,665 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!!