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

How do you include negations in grouping? I'm pretty sure that's the reason my code hasn't been accepted.

I'm being advised I've not got the right regex, but the request was 2 create 2 groups entitled email and phone using search, that omits the comma and space. I assume I should be able to use a negate here too. I've tried putting this in a verbose format but seem to have issues with my formatting. Do you have to tab a specific distance across from the edge in order for this to work?

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>\b[-\d\w.+]+@[-\d\w.]+)([,]\s)(?P<phone>\d{3}-\d{3}-\d{4})',string,re.I)

3 Answers

Eduardo Valencia
Eduardo Valencia
12,444 Points

To answer your first question, you include negations as normal in grouping ([^characters go here]). Perhaps the reason that it did not work for you is that you were including .+ after the negated set. By default, the negated set gets all characters not in the set, so the .+ would not be necessary. The (unnamed) group for the email part would look like this:

([^ ,]+@.+\.\w+)

Hi Eduardo

Thanks for helping, but I don't understand what you're suggesting with that answer for the unnamed group, are you meaning I use the code below?: That is not going to work, apologies but how would you write 'contacts' to get the desired result, e.g. with the emails and phone number whilst negating the comma and space?

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>\b[-\d\w.+]+@[-\d\w.]+)([^ ,]+@.+.\w+)(?P<phone>\d{3}-\d{3}-\d{4})',string,re.I)

print(contacts.groupdict())

Eduardo Valencia
Eduardo Valencia
12,444 Points
contacts = re.search('(?P<email>[^ ,]+@.+\.\w+).+(?P<phone>(\d{3}-){2}\d{4})', string)

What I meant was the pattern I gave you was that pattern you needed, except I did not add a name using (?P<email). The group that you made for the phone number appears correct; it was only the email one that needed fixing.

To negate the command and space, you should add [^ ,] (there is a space before comma). Remember that this will get any one character that is not a space or a comma, so you have to add the + after it to get the part of the email that is before the @ symbol. Also, instead of specifying the characters that you needed after the @ symbol manually and surrounding them with brackets, I just used .+ to get everything before the last period.

I was confused by this as well. I used the comma and space to separate the two groups and it worked for me:

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