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

Elizabeth McInerney
Elizabeth McInerney
3,175 Points

Twitter, 3 tries

I have tried to get twitter 3 different ways. Each is failing. Please show me why.

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

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

1 Answer

twitters = re.search(r'(?P<twitter>)',string) #named group - catches nothing now twitters = re.search(r'(?P<twitter>@[\w])',string) #we will catch @ and word after that. twitters = re.search(r'(?P<twitter>@[\w]$)',string, re.MULTILINE) # $ - for end of line, and re.MULTILINE so each line will be parsed

Elizabeth McInerney
Elizabeth McInerney
3,175 Points

Ok, this is what I do not get. Are you saying that you don't always use ^ and $ together? He used them together with re.M in the video. So if $ used together with re.M means look for something at the end of every line. What does ^$ used with re.M mean, or ^ used alone with re.M but without $?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

^ beginning of string/line

$ end of string/line

You can use them together or independently. They're just to indicate position in the string/line.