#!/usr/bin/env python # encoding: utf-8 # # Copyright (c) 2010 Doug Hellmann. All rights reserved. # """Match email addresses """ #end_pymotw_header import re address = re.compile( ''' [\w\d.+-]+ # username @ ([\w\d.]+\.)+ # domain name prefix (com|org|edu) # we should support more top-level domains ''', re.UNICODE | re.VERBOSE) candidates = [ u'first.last@example.com', u'first.last+category@gmail.com', u'valid-address@mail.example.com', u'not-valid@example.foo', ] for candidate in candidates: print print 'Candidate:', candidate match = address.search(candidate) if match: print ' Matches' else: print ' No match'