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 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)