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 trialAlexander Davison
65,469 Points"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
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
Treehouse Moderator 68,457 PointsWhen 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
Alexander Davison
65,469 PointsAlexander Davison
65,469 PointsIt is still producing the same error, sadly.
Here's my code:
Chris Freeman
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 PointsSorry. 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.
\s
added into the first_name and last_name character sets.^
is needed in front of the first group paren.Here is one solution:
Alexander Davison
65,469 PointsAlexander Davison
65,469 PointsThank you! I understand now.
However, I'm still confused on the ^ and why you need it... What's it for?
Chris Freeman
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 PointsThe caret
^
matches the beginning of the line or each line start in re.MULILINE. More at docsAlexander Davison
65,469 PointsAlexander Davison
65,469 PointsThanks!