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

Elizabeth McInerney
Elizabeth McInerney
3,175 Points

twitters

This is telling me that it is not working because it returns @kennethlove. but since that is a twitter address for the first person, I do not understand what the problem is. Also, I do not understand why this needs re.M, when the previous challenge did not.

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,re.M)

5 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

It needs re.M because the pattern should show that it's at the end of the line. You don't need to catch the space and you need a $ at the end to show end-of-line.

Elizabeth McInerney
Elizabeth McInerney
3,175 Points

ok, so I get that re.M turns the entire file into a bunch of strings for each line, and the ^$ is needed to tell it where the starting string begins and ends, but I guess I missed that re.M only tells the pattern to look at the end of the line. Looking back at the examples in the video, they looked at the whole line. So if there were multiple twitter addresses in each line, this code would only pull out the last one?

Elizabeth McInerney
Elizabeth McInerney
3,175 Points

What I got from playing around with your example in groups was that without ^$re.M, the search doesn't know when one line ends and another begins, so results get funky.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

No, you're correct that re.M finds each line in the string as its own string (effectively). Your pattern, though, will catch @kennethlove, not just @kennethlove.

Elizabeth McInerney
Elizabeth McInerney
3,175 Points

The first twitters below will print out @teamtree.....etc, but the second returns empty.

twitters = re.search(r'@[\w]+', string) print(twitters) twitters = re.search(r'^@[\w]+$', string, re.M) print(twitters)

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Right. The first finds the first @ it can, which will be in an email address. Not what we want.

The second looks for an @ at the start of a string, which doesn't exist.

Elizabeth McInerney
Elizabeth McInerney
3,175 Points

I am sorry, I don't get why my second re looks for the @ at the start of the line.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Because of the ^.

twitters = re.search(r'''^  # at the start of the line
@[\w]+  # look for @ followed by 1 or more consecutive word characters
$  # until you get to the end of the line
''', string, re.M|re.VERBOSE) 
Elizabeth McInerney
Elizabeth McInerney
3,175 Points

Ok, so your code above says that it keeps looking for the @[\w]+ until it gets to the end of the line. So I don't understand why my code didn't find anything. The only difference between your code and mine is that you wrote yours over several lines, hence the extra '' at the start and end of the string, and the re.VERBOSE flag. Correct? And I don't need those things if I am going to write the code in one line, correct?

Thanks for your help! Hey it looks like the teacher needs some more points! There are lots of people beating you on here!

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Your code and my code are exactly the same except for the verbose flag and the multiple lines. Yours didn't find anything because it says the line has to start with ^, and none of the lines start with that. It also says there can't be anything but @ and word characters (no spaces) all the way until the end of the line. Again, this doesn't match any of our lines.

Your code is actually fine with one change. Delete the ^. That way it'll only find the @ and word characters that are at the end of the line.

As for points, I'm not really concerned about them. I'm pretty sure I know what's going on. ;)

Elizabeth McInerney
Elizabeth McInerney
3,175 Points

Hah! Yes, you do! I was just joking with you :-)