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

Y B
Y B
14,136 Points

REgex names

Hmm I can't get any of these right now. so I must be misunderstanding something?

names.py
import re

string = 'Perotto, Pier Giorgio'

names = re.match('r([\w]*) ,([\w]+)', string)

Watch out for leading space of first name. The second capture will get ' Pier Giorgio' with leading space, which is wrong. names = re.match(r'^(\w+),\s([\w\s]+)$', string)

13 Answers

Daniel Muchiri
Daniel Muchiri
15,407 Points

Better formatted solution is:

import re

string = 'Perotto, Pier Giorgio'

names = re.match(r'''
    ^(?P<last_name>[\w]*),\s                # Last name
    (?P<first_name>[\w\s\w]*)$           # First name
''', string, re.X)
Kerrick Hahn
Kerrick Hahn
5,728 Points

Tried the exact same thing, didn't work. Also copy and pasted this code to see if maybe I was missing a small error, but nope. Returns a "Bummer: Hmm, no .groups() attribute. Did you do a match?"

import re

string = 'Perotto, Pier Giorgio'

names = re.match(r'([\w]*), ([\w]+ [\w]+)', string)

print(names)

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

That first name has a space in it and your pattern doesn't catch spaces...

Ok so here it goes the solution

names = re.match(r'^(?P<firstname>[\w]*),\s(?P<lastname>[\w\s\w]*)',string,re.X|re.M) 

I am pretty sure you can skip the re.M but well it was there

Y B
Y B
14,136 Points

I must be doing something wrong more than that as it doesn't work - I can't get anything to pick up even if I just try last name only, first names only...... It always seems to return None for some reason?

import re

string = 'Perotto, Pier Giorgio'

names = re.match('r([\w]*), ([\w]+ [\w]+)', string)

print(names)
Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Which side of the quote mark should your r be on?

Challenge Task 1 of 1

Create a variable names that is an re.match() against string. The pattern should provide two groups, one for a last name

match and one for a first name match. The name parts are separated by a comma and a space.

import re

string = 'Perotto, Pier Giorgio'

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

Y B
Y B
14,136 Points

Hmm I still can't get this to work

I used:

names = re.match('r([\w]*) ,([\w]+ [\w]+)', string)

which I interpret as (for the first name) a block of 1 or more letters followed by a space and another block of one or more letters.

Y B,

The 'r([\w]*) , ([\w....... is looking for a word AND a space before the comma.

Ron

Y B
Y B
14,136 Points

Aaargh! Thank you......

This question is referring to the following 'Name Groups" challenge: http://teamtreehouse.com/library/regular-expressions-in-python/introduction-to-regular-expressions/name-groups

"Challenge Task 1 of 1 Create a variable names that is an re.match() against string. The pattern should provide two groups, one for a last name match and one for a first name match. The name parts are separated by a comma and a space."

Alan Ng
Alan Ng
13,368 Points

first of all, the r tag should be out side of the quotation mark second, you need to consider the space in between Pier and Giorgio

darcyprice
darcyprice
4,442 Points

Hello,

I am also having trouble with this Challenge.

First, I have tried the following code and it returns 'None':

names = re.match(r''' ([\w]),\s([\w\s\w]) ''', string, re.X)

I even copied the following code from this post and it also returned 'None':

names = re.match(r''' ^(?P<last_name>[\w]),\s (?P<first_name>[\w\s\w])$ ''', string, re.X)

I assume that I am missing something blindly obvious to make it return None, does anyone know what I'm doing wrong?

The second issue I'm having is that has an error saying: 'Module 're' has no 'X' member). Why is that? I am running the code on Repl.it on Python 3.6.1 if that relevant.

import re

string = 'Perotto, Pier Giorgio' names = re.match(r'([\w]*),\s([\w ] *)', string, re.X | re.M)