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 Groups

Maxim Andreev
Maxim Andreev
24,529 Points

Regex challenge

Can't figure out what I am missing...

emails.py
import re

string = '''Love, Kenneth, kenneth+challenge@teamtreehouse.com, 555-555-5555, @kennethlove
Chalkley, Andrew, andrew@teamtreehouse.co.uk, 555-555-5556, @chalkers
McFarland, Dave, dave.mcfarland@teamtreehouse.com, 555-555-5557, @davemcfarland
Kesten, Joy, joy@teamtreehouse.com, 555-555-5558, @joykesten'''

contacts = re.search(r'''
    ^(?P<email>[\w.-+]+@[\w.-]+)?  # email
    (?P<phone>\d{3}-\d{3}-\d{4})?$  # phone
    ''', string, re.X|re.M)

2 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

You probably got an error about a bad range. The re engine is weird about hyphens. Move the hyphens you're using to the beginning of their sets. That'll clear up that error.

Next, though, you're gonna get an error about not getting a search object. You're using ^ and $ in your pattern when your pattern doesn't actually match the beginning or end of the string. Don't use those for this challenge.

Maxim Andreev
Maxim Andreev
24,529 Points

Yes I realize it now..since hyphens are for ranges too like [a-z] it messes with the code. Thanks!

But I don't think that I fully understand ^$ and re.MULTILINE then.

How would you specify that each tuple should only be for each individual line?

edit: I understand ^$ re.M now... re.M re.MULTILINE When specified, the pattern character '^' matches at the beginning of the string and at the beginning of each line (immediately following each newline); and the pattern character '$' matches at the end of the string and at the end of each line (immediately preceding each newline). By default, '^' matches only at the beginning of the string, and '$' only at the end of the string and immediately before the newline (if any) at the end of the string.

however I am still unclear of how to make sure the tuple only is created on a line by line basis.

edit 2: Nevermind figured it out, thanks

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Mind sharing your understanding for those that follow this thread later?

Maxim Andreev
Maxim Andreev
24,529 Points

The re.M flag takes care of it Kenneth Love, and makes it so that each new line is treated as a new string. While the parenthesis adds the individual items within that tuple.