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

Trace Harris
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Trace Harris
Python Web Development Techdegree Graduate 22,065 Points

I just need someone to break this down for me I am getting half matches etc not all sure why I am not matching patterns

please help me break this step by step because I am at a loss why this is not working

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<last_name>[\w]+?\s?[\w]+),\s
#(?P<first_name>[\w]+):\s
#(?P<score>[\d+])
""", string, re.X)
Trace Harris
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Trace Harris
Python Web Development Techdegree Graduate 22,065 Points

able to get match object for first name last_name match object is 'land' I am not sure why I am not matching McFarland even when I was using re.I flag

1 Answer

Dave StSomeWhere
Dave StSomeWhere
19,822 Points

Yep - regex is difficult for me also - that's why it helps to review your code and debug it - help me gain a better understanding also.

This won't be enough to complete the challenge but should be enough to get you moving in the right direction - the challenge will also require the begin and end line identifiers along with handling multiple names (like Stewart Pinchback - two first names) - Just let me know if you need assistance with those.

Three key items:

  1. the "r" for raw string needs to be before the triple quotes
  2. the '+' plus sign for 1 or more needs to be outside the brackets for the score (or you'll only get the first digit)
  3. you also need re.M for multiline -

With just those changes you should be able to test the following in your local environment like below:

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]+),\s
(?P<first_name>[\w]+):\s
(?P<score>[\d]+)
""", string, re.M|re.X)

print(players)
# output
<_sre.SRE_Match object; span=(0, 17), match='Love, Kenneth: 20'>
print(players.groups())
# output
('Love', 'Kenneth', '20')
print(players.groupdict())
# output
{'last_name': 'Love', 'first_name': 'Kenneth', 'score': '20'}

Hope that helps