Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Regular Expressions in Python Introduction to Regular Expressions Groups

fahad lashari
fahad lashari
6,141 Points

For 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
MOD
Chris Freeman
Treehouse Moderator 67,778 Points

Always 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
fahad lashari
fahad lashari
6,141 Points

Ah that's brilliant. Thanks!

Gabija Bakonytė
Gabija Bakonytė
2,097 Points

What is the purpose of triple quatation marks here?

Chris Freeman
Chris Freeman
Treehouse Moderator 67,778 Points

The 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
justlevy
4,772 Points
names = 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
Chris Freeman
Treehouse Moderator 67,778 Points

Correct! The \w says all word characters [a-zA-Z]. Since this covers all cases (unintentional pun), ignoring case becomes moot.