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
triplemalt
1,084 PointsPython regular expression and grouping
import re
p = re.compile(r'\w+@(\w)+\.com')
m = p.findall('''From: example1@email.com
To: example2@email.com''')
I'm trying to understand how grouping works in regular expression. In the above example, why did the email address not get returned but works fine without the parenthesis?
[MOD: corrected formatting -cf]
1 Answer
Chris Freeman
Treehouse Moderator 68,468 PointsGreat question. Based on the re.findall docs, If one or more groups are present in the pattern, return a list of groups.... Empty matches are included in the result unless they touch the beginning of another match.``
The + outside the group implies more than one group. Since the group is a single \w character, and the "unless they touch the beginning of another match*" comment, it seems that only the last matching single character is being returned. In the code sample, this it the "l" before the ".com" in both email addresses.
If you want the domain name, move the + in side the group:
>>> p = re.compile(r'\w+@(\w+)\.com')
>>> m = p.findall('''From: example1@emaiX.com
... To: example2@emaiY.com''')
>>> m
['emaiX', 'emaiY']
Post back if you have more questions. Good luck!!