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

Keenan Smith
Keenan Smith
Python Development Techdegree Student 5,487 Points


I dont know how to limit the expression to just get the 3 emails from the list. I keep getting anything with a @ in front of it.

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']

def find_emails(data):
    return (r'[a-zA-Z0-9.-+]+@[a-zA-Z0-9.-]+\.[a-zA-Z0-9-.]+', data)

1 Answer

Jeff Muday
Jeff Muday
Treehouse Moderator 26,998 Points

Regex is one of the trickier things to work on. And email recognition/parsing is notorius for being incredibly complex.

You seem to have gotten really close to a solution, let's build on that.

First, your return statement is in the correct configuration, but you will need to use the function re.findall(expr, data)

This gets you solution going!

def find_emails(data):
    return re.findall(r'[a-zA-Z0-9.+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z0-9-.]+', data)

We can make it a little more robust by using the \b character to look for BOUNDARIES (e.g. whitespace and commas) between distinct emails.

Here's a reference document: https://www.regular-expressions.info/wordboundaries.html

We can improve it by putting a \b right up front in the expression:

def find_emails(data):
    return re.findall(r'\b[a-zA-Z0-9.+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z0-9-.]+', data)