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

benjamin siverly
benjamin siverly
3,430 Points

This works fine on pythex.org, but won't work to capture Twitter on task 2 of 2

Sorry to bug - I got past the first task, but now the second won't work. I checked my regex search on pythex.org and used the exact phrase I made, but I'm getting the 'try again' error, which doesn't seem.

I'm needing to make a re.search() for the twitter handles (starting with '@' and not including space before) which are at the at the end of the lines (so my code ends in '$') and uses re.MULTILINE to make sure it was grabbed...

Still can't seem to get it to pass....

Kenneth Love

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

twitters = re.search(r'''
    @\w+$
''', string, re.MULTILINE)

return contacts, twitters

2 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

I'm not sure why you're constantly using the multi-line strings. I don't think they'll affect it but I wouldn't add that extra bit of complexity in there. FWIW, r'(?P<twitter>@\w+)$' passed for me. I only used the re.M flag.

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

You don't add multiple flags with commas (remember, these are bits so we have to use bitwise operations on them. That got called out in the videos). Secondly, you're using a multi-line regex pattern without using the re.X flag, so it's looking for newlines.

benjamin siverly
benjamin siverly
3,430 Points

Hmm... Not sure how I got the first bit to run before... Fixed that.

Still unable to get the second part to pass.

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