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 trialY B
14,136 PointsREgex names
Hmm I can't get any of these right now. so I must be misunderstanding something?
import re
string = 'Perotto, Pier Giorgio'
names = re.match('r([\w]*) ,([\w]+)', string)
13 Answers
Daniel Muchiri
15,407 PointsBetter 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
5,728 PointsTried 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?"
Annie Scott
27,613 Pointsimport re
string = 'Perotto, Pier Giorgio'
names = re.match(r'([\w]*), ([\w]+ [\w]+)', string)
print(names)
Kenneth Love
Treehouse Guest TeacherThat first name has a space in it and your pattern doesn't catch spaces...
tomasvukasovic
24,022 PointsOk 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
Daniel Muchiri
15,407 PointsThanks Tomas
Y B
14,136 PointsI 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
Treehouse Guest TeacherWhich side of the quote mark should your r
be on?
Ary de Oliveira
28,298 PointsChallenge 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
14,136 PointsHmm 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.
Ron Fisher
5,752 PointsY B,
The 'r([\w]*) , ([\w....... is looking for a word AND a space before the comma.
Ron
Y B
14,136 PointsAaargh! Thank you......
james white
78,399 PointsThis 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."
Kenneth Love
Treehouse Guest TeacherYou have a question?
Alan Ng
13,368 Pointsfirst 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
4,442 PointsHello,
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.
Kalkidan Abebe
4,178 Pointsimport re
string = 'Perotto, Pier Giorgio' names = re.match(r'([\w]*),\s([\w ] *)', string, re.X | re.M)
MING JIA
1,280 PointsMING JIA
1,280 PointsWatch 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)