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

Great! Now, make a new variable, twitters that is an re.search() where the pattern catches the Twitter handle for a pers

Pliz Help, I don't know where I'm getting this wrong, i'm getting Bummer! Didn't get the right capture. Got "@joykesten". Please help

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)


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

2 Answers

Bart Bruneel
Bart Bruneel
27,212 Points

Hello Edwin,

I think the problem is the inclusion of ?$ at the end of your regex. I solved the problem like this:

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

It is about the same solution. @[\w\d]+ selects every letter following an @ . To escape inclusion of the @teamtreehouse e-mail adresses, I've included [^t]. This worked, but is probably not the most elegant solution.

An amazingly handy website to check regex-patterns, which I used for nearly every challenge in this course is http://www.regexr.com .

Happy coding!

thanks it passed

thanks it passed

although your solution 'works', i.e. it passes, it doesn't do it in the fashion requested. The instructions say '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.'

that part about being at the end of the string is how you avoid picking up the '@treehouse' Although you found a work-around with [^t], it is not what the challenge intended. basically it is just tricking the challenge by returning the correct output but not actually doing the requested steps.

My question is how do I 'mark it as being at the end of the string' I was under the impression that the '$' was for that purpose but don't understand the implementation of it. This certainly was not gone over well in Kenneth's video. merely mentioned.

(\s@[\w]+)

Fixes the [^t]

Christian Kuylen
Christian Kuylen
3,176 Points

While the best answer completed the challenge, it wasn't the intended goal. You're supposed to use '$' to signify the twitter handles are at the end of the string. But we also want it to look at the end before a new line, which is why you have to use the multiline flag.

The only problem was the '?' before the $. I think it was because you copied the code from the video, which used the ? because some of the entries in the big string didn't have a twitter account, and so it made them optional. I don't know why having this makes the challenge fail, because it does find all the twitter handles, but removing it seems to do the trick, as well as accomplish the intended challenge.

```twitters = re.search(r''' (?P<twitter>@[\w\d]+)$ # Removed the ? before the $ ''', string, re.X|re.M) # Added the multiline flag

Christian Kuylen
Christian Kuylen
3,176 Points
twitters = re.search(r''' (?P<twitter>@[\w\d]+)$  
''', string, re.X|re.M) #Added the multiline flag

Fixed some input errors.