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

Goals challenge

Great! Now, make a new variable, twitters that is an re.search() where the pattern catches the Twitter handle for a person. Remember to mark it as being at the end of the string. You'll also want to use the re.MULTILINE flag.

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)

# TASK 2 CODE
twitters = re.search(r'''
    (?P<twitter>@[\w\d]+)
''', string, re.X)

Regular Expressions in Python

Bummer! Didn't get the right capture. Got "@teamtreehouse".

So I am having a problem with task 2. This is my code but all I get is @treamhouse and I don't get the other twitter handles. any idea why?

4 Answers

Add '$' to end of pattern; Matches the end of the string or just before the newline at the end of the string, and in MULTILINE mode also matches before a newline.

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

where are you placing re.M? =)

When I add the re.M at the end it tells me "Bummer! twitters doesn't seem to be a regex search object."

EDIT: NVM got it. I added $ at the end of the string thanks to Perry Stripling

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

don't forget give a twitter name for the expresión after ?P

I don't know where I getting it wrong but its refusing to pass for me,