Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Regular Expressions in Python Introduction to Regular Expressions Email Groups

Anthony Grodowski
Anthony Grodowski
4,902 Points

Multiline problem

Like the bummer that I get is literally saying what the answer is but still I'm not passing that - Didn't get the right capture. Got "@kennethlove ".. In my workspace everything works perfect. Also few things remain unclear for me - what is the purpouse of using re.MULTILINE? Is it just to make python understamd the end of the line as the end of the string we're looking for? Another thing is the usage of ^ and ? at the beggining and at the end of the code. Kenneth said that they're marking the beggining and the end of the string but what does it exactly mean?

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.MULTILINE)

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

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,736 Points

The flag re.MULTILINE says:

When specified, the pattern character '^' matches at the beginning of the string and at the beginning of each line (immediately following each newline); and the pattern character '$' matches at the end of the string and at the end of each line (immediately preceding each newline).

Using re.search will always return the first match only, if found. In this case, the re.MULTILINE allows the search to treat the string as broken in to segemnts at the NEWLINE characters \n and search each segment independently. This allows the ^ and $ to be reused on each segment.

I like to view the ^ and $ as "pattern anchors" that force the pattern match to have to include either the beginning or the end (or both) of the target string. These anchors do not consume any characters from the target string, but have a matching position that must match.

Post back if you have more questions. Good luck!!