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

Create a function named find_emails that takes a string. Return a list of all of the email addresses in the string.

Create a function named find_emails that takes a string. Return a list of all of the email addresses in the string.

multiple repeats, how is this happening? please help =)

sets_email.py
import re
def find_emails(str):
  return re.findall(r'\b[trehous]+{9}\b', str)
# 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']

8 Answers

Grzegorz Gancarczyk
Grzegorz Gancarczyk
10,064 Points

This challenge will pass with this code: (something very similar to what MUZ140953 wrote but I promise I hadn't looked on this when I was writing my code :) )

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

Just don't try to add these addresses to a new list and return it as I was trying to :)

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Why are you only searching for the letters "trehous" (occurring as a group of 9 characters) instead of searching for email addresses?

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

thus how i had make it try this one

def find_emails(data):
  result = re.findall(r'\w*.?\w+@\w+.\w+.?\w+',data)
  return result

lol my regex just playing with *,+,?,\w not yet learn about [] and dot(.) in regex

Answer

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(str): return re.findall(r'[-\w\d+.]+@[-\w\d.]+', str)

import re
def find_emails(str):
  return re.findall(r'\b[@]+{1}\b', str)

... my logic was that the any email must have the word treehouse... new logic says that any email has to have the at symbol... why does my new logic have a "multiple repeat" if I only specified one "at" symbol?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Because you told it to? Code doesn't just suddenly decide to do whatever it wants :)

Your pattern, right now, says "find a word boundary, then 1 or more @ symbols, then...something. A count of 1? And then another word boundary".

That's not anything at all like an email address. We find email addresses before this point in the videos. Should probably go back and watch that part again.

found the proper format in the sets video... I am always trying to make programming as easy, short and fast as possible, do you end up doing the same for your professional projects?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

You're learning. Don't worry about any of those three. Take as much time as needed to figure out a problem. Write as much code as you need to to make it work.

Kenneth Love
Hey all I got the answer and could pass the test thanks to you all but still I am confused about

[-\w\d+.]+@[-\w\d.]+

whats the meaning of the + in above code which is there after both of [] ?

Timothy Mattingly
Timothy Mattingly
14,436 Points

The + is saying to find at least one or more of the characters from the set.