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 Name Groups

Jose Soto
Jose Soto
23,407 Points

Why is my Regex challenge answer wrong?

I cannot figure out this regular expressions challenge. All the resulting values seem correct, but the challenge keeps saying that I did not get the correct values.

Here is the output results and they seem correct to me:

ln:

Perotto,

fn:

Pier Giorgio

Am I missing anything?

names.py
import re

string = 'Perotto, Pier Giorgio'

names = re.match(r'''
  (?P<lastname>^[\w]+,)
  (?P<firstname> [\w ]+$)
''', string, re.X)

ln = names.groupdict().get('lastname')
fn = names.groupdict().get('firstname')

4 Answers

Jose Soto
Jose Soto
23,407 Points

I figured it out through trial and error. I ran a ton of tests on my local python environment.

You can express a comma and a space within a group a couple of ways:

test = re.match(r'''
(?P<group_name>,\s)   # \s is a space symbol
''', string, re.X)

or

test = re.match(r'''
(?P<group_name>,[ ])  # there is a space between the [] symbols
''', string, re.X)

However, in order to complete this challenge, you should not include the comma and space between the first and last name. You can do this by placing the comma and space outside of the group parenthesis:

test = re.match(r'''
(?P<last_name>regex),    #comma is after the end parenthesis
\s(?P<first_name>regex) #space symbol is before the open parenthesis
''', string, re.X)
[\w]*)

how does it work now?

=)

Jose Soto
Jose Soto
23,407 Points

Hi Michaelangelo. Unfortunately, that did not work for me. It turns out I was reading the response wrong from the challenge. It was telling me that the first name should not have a comma and the last name should not have a space. I think that output should be re-worded.

I figured it out. You can use characters to match a phrase in a string, but not actually include them in the result by placing them outside of the group parenthesis. For example, if there is a space right before my needed result, but I don't want to grab it (just use it to locate the result), then I can use something like this:

[\s](?P<groupname>regex query)

I hope that helps others.

Jiecao Wang
Jiecao Wang
5,833 Points

So you are saying the question meant "first name should not have a comma and the last name should not have a space. "

  1. How did you figure this out?

  2. I am not sure how to express the complement of a comma and a space within the group