Help with Regular Expressions matching

David LeBlanc whisper at oz.net
Fri Jan 31 04:32:19 EST 2003


> -----Original Message-----
> From: python-list-admin at python.org
> [mailto:python-list-admin at python.org]On Behalf Of Ian Bicking
> Sent: Friday, January 31, 2003 0:32
> To: ajit at cc.iitb.ac.in
> Cc: python-list at python.org
> Subject: Re: Help with Regular Expressions matching
>
>
> On Fri, 2003-01-31 at 01:26, ajit k jena wrote:
> > Someone in mailman mailing list suggested a regular expression of the
> > form:
> >
> > 	^.*?@(?!your\.domain\.com$)
>

import re

pat = r'^.*?@\w?\.?domain\.com$'

mat = re.compile(pat)

m = mat.match('a at b.domain.com')
if m: print 'b',  m.group(0) # match

m = mat.match('a at c.domain.com')
if m: print 'c', m.group(0) # match

m = mat.match('a at domain.com')
if m: print 'domain', m.group(0) # match

m = mat.match('a at site.com')
if m: print 'no', m.group(0) # no match - no output

m = mat.match('a at mydomain.com')
if m: print 'no', m.group(0) # no match - no output

m = mat.match('a at .domain.com')
if m: print 'oops', m.group(0) # match - illegal email address.

will match any at something.domain.com, any at .domain.com (oops!) or
any at domain.com exactly.

Writing little test scripts like this with the re doc open in a webbrowser
is a great way to figure out how re's work. Note the one case where
any at .domain.com matches incorrectly should be tested for separately if
necessary (probably not since I don't think it will route in the first
place).

HTH,

Dave LeBlanc
Seattle, WA USA






More information about the Python-list mailing list