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 Email

Christian Mangeng
Christian Mangeng
15,970 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 68,427 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,970 Points

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