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.
Keenan SmithPython 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("firstname.lastname@example.org, @support, email@example.com, firstname.lastname@example.org") # ['email@example.com', 'firstname.lastname@example.org', 'email@example.com'] def find_emails(data): return (r'[a-zA-Z0-9.-+]+@[a-zA-Z0-9.-]+\.[a-zA-Z0-9-.]+', data)
Jeff MudayTreehouse 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
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)