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

Lewis Cowles
Lewis Cowles
74,902 Points

Can you please re-phrase the Question lol

Hi Would it be possible to re-phrase the question for Task 2 of 2 here.

On several occasions I had completed it as far as extracting all three parts of the string, but I did not quite get two things

a) twitters was to be another variable and not a named selector in the regex b) that you wanted the newline in the search of the twitter handle... This just seemed wrong, I went and created a workspace in the end it said everything was fine and I was like GAHHHHHH!

Eventually I worked out what the requirement was, but I don't think it helped my python, in fact now I'm unsure about some of the work I was doing, so I think it might be lovely to reword it a bit?

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

Mentioning Kenneth Love to make sure he sees this.

Lewis Cowles
Lewis Cowles
74,902 Points

Thx Geoff, hope you had an awesome Christmas and are soon to start a great new year

1 Answer

I agree about the re-wording for twitters.

I really struggled with this...

This code kept giving me errors. I think it has something to do with the fact that both were multiline. It kep saying that challenge one was not passing after I added the twitter re.search:

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

twitters = re.search(r'''
    (?P<twitter>@[\w\d]+)?$  # Twitter
''', re.X|re.M)

..so finally I made twitters into a single line and put it before the line that passed challenge one (in order to get challenge 2 to pass):

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

twitters = re.search(r'(?P<twitter>@[\w\d]+)$', string, re.M)

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

The Python Regex course was not really a very 'happy camper' Treehouse course for me..

Lewis Cowles
Lewis Cowles
74,902 Points

Hi James,

I loved the course, I think Kenneth always injects a lot of passion into his projects and for me that always shows up. Please remove the answer as the point of the post was not supposed to be asking for answers, more seeing how many people found the same issue(s) as me and highlighting it for Kenneth should the course get revised or patched.

Btw nice score!