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

Christopher Earle
PLUS
Christopher Earle
Courses Plus Student 22,340 Points

Python RegEx: Create a variable named players ...

Hi,

I'm trying to create code using search() or match() to return the first name, last name, and score. In a local environment, I successfully captured all groups correctly by replacing search() with findall() and concatenating the results as players = pattern1matches + pattern2matches + pattern3matches. However, I'm not sure how to accomplish the task using search() or match() because '+' is an unknown operator for match objects. Also, I'm sure that there's a cleaner solution to accomplish the task, I just don't know what it is ...

I'd really appreciate any help!

players.py
import re

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

pattern1 = re.compile(r"""
  (?P<last_name>\w+)
  ,\s
  (?P<first_name>\w+)
  :\s
  (?P<score>\d+)$
  """, re.X|re.M)

pattern2 = re.compile(r"""
  (?P<first_name>^\w+)
  \s
  (?P<last_name>\w+)
  ,\s\w+\s\w+:\s
  (?P<score>\d+)$
  """, re.X|re.M)

pattern3 = re.compile(r"""
  (?P<first_name>\w+)
  \s
  (?P<last_name>\w+)
  :\s
  (?P<score>\d+)$
  """, re.X|re.M)

pattern1matches = pattern1.search(string)
pattern2matches = pattern2.search(string)
pattern3matches = pattern3.search(string)

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

One element of this challenge that gets misinterpreted is the last entry. It is a person with two last names and two first names. (what? yes!)

When applying search patterns, using search() will only return the first item found, using match() will only return the first item found starting from the first character. To find all of the non-overlapping matches use findall().

>>> print(pattern1.findall(string))
[('Love', 'Kenneth', '20'), ('Chalkley', 'Andrew', '25'), ('McFarland', 'Dave', '10'), ('Kesten', 'Joy', '22')]

You can compile your patterns into a single pattern by combining character variances into sets using [ ] as follows:

pattern_all = re.compile(r"""
  (?P<last_name>[\w ]+)
  ,\s
  (?P<first_name>[\w ]+)
  :\s
  (?P<score>\d+)$
  """, re.X|re.M)

print(pattern_all.findall(string))

Running in interactive Python:

$ python3
Python 3.4.3 (default, Oct 14 2015, 20:28:29) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> 
>>> string = '''Love, Kenneth: 20
... Chalkley, Andrew: 25
... McFarland, Dave: 10
... Kesten, Joy: 22
... Stewart Pinchback, Pinckney Benton: 18'''
>>> 
>>> pattern_all = re.compile(r"""
...   (?P<last_name>[\w ]+)
...   ,\s
...   (?P<first_name>[\w ]+)
...   :\s
...   (?P<score>\d+)$
...   """, re.X|re.M)
>>> 
>>> print(pattern_all.findall(string))
[('Love', 'Kenneth', '20'), ('Chalkley', 'Andrew', '25'), ('McFarland', 'Dave', '10'), ('Kesten', 'Joy', '22'), ('Stewart Pinchback', 'Pinckney Benton', '18')]
>>> 
Christopher Earle
PLUS
Christopher Earle
Courses Plus Student 22,340 Points

Chris,

Thanks so much for the hint and helpful info. Worked like a charm!