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 trialfahad lashari
7,693 PointsFor anyone who is stuck on the next code challenge.
import re
string = 'Perotto, Pier Giorgio'
names = re.match(r'''([-\w]+),\s([\w ]+[\w]+)''', string, re.I)
This is what you need to do. Most people are forgetting to use the re.I function. And also for the first names, it's simpler and easier to just put the space explicitly after the first name.
Hope this helps. Study as to why it passes the challenge. Re-do the challenge yourself a few times without looking at this. To really re-enforce the knowledge.
kind regards,
Fahad
3 Answers
Chris Freeman
Treehouse Moderator 68,441 PointsAlways look for opportunities to simplify regexs. The first name group can be simplified:
[\w ]+[\w]+ # "one or more of word chars
# and spaces, followed by one
# or more word chars" reduces to
[\w ]+ # one or more of word chars and spaces
Gabija Bakonytė
2,097 PointsWhat is the purpose of triple quatation marks here?
Chris Freeman
Treehouse Moderator 68,441 PointsThe triple quotation marks allow multi-line strings to be defined. In this case, the string does not contain any newline characters so a single quote could be used. See examples under Strings in the docs.
justlevy
6,325 Pointsnames = re.match(r'([\w]+),\s([\w\s]+)', string)
I used this code and it was accepted. I guess the re.I
isn't necessary.
Chris Freeman
Treehouse Moderator 68,441 PointsCorrect! The \w says all word characters [a-zA-Z]. Since this covers all cases (unintentional pun), ignoring case becomes moot.
fahad lashari
7,693 Pointsfahad lashari
7,693 PointsAh that's brilliant. Thanks!