#!/usr/bin/env python # encoding: utf-8 # # Copyright (c) 2010 Doug Hellmann. All rights reserved. # """Negative look behind assertion. """ #end_pymotw_header import re address = re.compile( ''' ^ # An address: username@domain.tld [\w\d.+-]+ # username # Ignore noreply addresses (?<!noreply) @ ([\w\d.]+\.)+ # domain name prefix (com|org|edu) # limit the allowed top-level domains $ ''', re.UNICODE | re.VERBOSE) candidates = [ u'first.last@example.com', u'noreply@example.com', ] for candidate in candidates: print print 'Candidate:', candidate match = address.search(candidate) if match: print ' Match:', candidate[match.start():match.end()] else: print ' No match'