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 Groups

fahad lashari
fahad lashari
7,693 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 68,441 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
7,693 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 68,441 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
6,325 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 68,441 Points

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