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

Keenan Smith
seal-mask
.a{fill-rule:evenodd;}techdegree
Keenan Smith
Python Development Techdegree Student 5,487 Points

find_email

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.

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

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

1 Answer

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,720 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)