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

matthias papsdorf
PLUS
matthias papsdorf
Courses Plus Student 4,152 Points

Exercise 2: twitter handler

Hey there,

I am struggling a bit with this exercise. I successfully created a re.search for the email and number. I seem to have a problem with doing the same with twitter. Even though I get the twitter handler it tells me that it is the wrong output. Maybe I missunderstood the question. Could you please have a look

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("(?P<email>[\w\+]+@[\w\.]+), (?P<phone>[\d-]{12})", string)
twitters = re.search("\s@\w+", string, re.MULTILINE)

1 Answer

Michael Norman
PLUS
Michael Norman
Courses Plus Student 9,399 Points

Your answer is very close. Two small issues though, the way you currently have it the regex does return the twitter handle but you may notice that there is a space in the front of the handle. ie: " @Kennethlove". To fix this just remove the "\s" from the front of the regex since you don't want to accept that space.

The second issue is from this part of the instructions "Remember to mark it as being at the end of the string." If you recall from the video we can use the '^' to mark the beginning of the string and a '$' to mark the end. So with all of this, the correct regex would be

twitters = re.search("@\w+$", string, re.MULTILINE)
matthias papsdorf
matthias papsdorf
Courses Plus Student 4,152 Points

Hi Michael,

That was indeed my mistake. Thank you very much for the quick answer.

Regards, Matthias