#! /usr/bin/python """ Module email2 Contains a single function extract_email_addr() that returns a sorted list of the uniue email addresses found in the string which is it's single argument. If called as a script, it applies this function to the text from stdin and then prints the email addresses found to stdout, one to a line """ import re from sys import stdin # this pattern approximates a legal email address pat = re.compile(r'[-\w][-.\w]*@[-\w][-\w.]+[a-zA-Z]{2,4}') def extract_email_addr(text): """returns a sorted list of the uniue email addresses found in the string which is it's single argument""" found = set() for address in pat.findall(text): # the add() method adds an element to a set object found.add(address) return sorted(found) def main(): # invoke this if called from a script to get text from stdin and # print output to stdout for address in extract_email_addr(stdin.read()): print address # This will be true of the file was called as a script if __name__ == "__main__": main()