
"""
Group Extraction

The "group" feature of a regular expression allows you to pick out parts of the matching text.
 Suppose for the emails problem that we want to extract the username and host separately. 
 To do this, add parenthesis ( ) around the username and host in the pattern, like this: r'([\w.-]+)@([\w.-]+)'. 
 In this case, the parenthesis do not change what the pattern will match, 
 instead they establish logical "groups" inside of the match text.
 On a successful search, match.group(1) is the match text corresponding to the 1st left parenthesis, 
 and match.group(2) is the text corresponding to the 2nd left parenthesis. 
 The plain match.group() is still the whole match text as usual.
"""
import re
str = 'purple alice-b@google.com monkey dishwasher'
match = re.search('([\w.-]+)@([\w.-]+)', str)
if match:
    print match.group()   ## 'alice-b@google.com' (the whole match)
    print match.group(1)  ## 'alice-b' (the username, group 1)
    print match.group(2)  ## 'google.com' (the host, group 2)