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

My regex expression is not parsing correctly for twitter handles

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.

My output, I think, is getting messed up with the email addresses. It appears to be outputting empty strings with the twitter handles.

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

I found my error. I needed to remove the ? in front of the $. Everything works now.

Johannes Scribante
Johannes Scribante
19,175 Points

Well done! Does someone perhaps know why this would give an error? Would it not just make the group optional?

1 Answer

Oszkár Fehér
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Oszkár Fehér
Treehouse Project Reviewer

Hi William My opinion, this is the most delicate part of python and the expressions are very precise, the best way to experiment a lot with this, as many cases possible, but this is the part what you don't need to learn word by word, it's enough to know how to look up the documentations. This line will help you to pass the quiz

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

But I also noticed that the first quiz you solved well. I hope this helps you Keep up the good work, and nice job.