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 Email

Christian Mangeng
Christian Mangeng
15,969 Points

Question to Python regular expressions (find_emails challenge)

Hi Community,

I have a special question to this challenge. I'm not interested in the actual solution of the challenge, as I've already solved it using:

def find_emails(my_string):
    return re.findall(r'[-\w\d+.]+@[-\w\d.]+', my_string)

But what confused me about it at the beginning was the example output given (see below), in which the ".love" and "+case" are missing in the first and third address. Now my question is: Is there a way to output the emails EXACTLY like in the example given, using regular expressions? It would mean to ignore the . and + characters of the first part of the email, as well as the following letters before the @.

sets_email.py
import re

# Example:
# >>> find_email("kenneth.love@teamtreehouse.com, @support, ryan@teamtreehouse.com, test+case@example.co.uk")
# ['kenneth@teamtreehouse.com', 'ryan@teamtreehouse.com', 'test@example.co.uk']

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,763 Points

If I'm understanding your question correctly, there is not a way to directly get the exact example output show in the challenge.

# Example:
# >>> find_email("kenneth.love@teamtreehouse.com, @support, ryan@teamtreehouse.com, test+case@example.co.uk")
# ['kenneth@teamtreehouse.com', 'ryan@teamtreehouse.com', 'test@example.co.uk']

The regex acts like a filter that either matches or doesn't. If it matches it outputs the match, otherwise no output. To drop the ".love" from the email, that is, everything between the last period of an address and the at-sign, the regex would need to define a group for each desired section then reassemble the parts to form a whole address.

Your question has me wondering if the example code and results are out of date in the challenge and may need updating. Tagging Kenneth Love to review the example code in the challenge for correctness.

Christian Mangeng
Christian Mangeng
15,969 Points

Yes, it's what I was asking for. Thank you, that's good to know!