Help with Regular Expressions matching
Peter Abel
p-abel at t-online.de
Sat Feb 1 18:45:45 EST 2003
"David LeBlanc" <whisper at oz.net> wrote in message news:<mailman.1044005637.3191.python-list at python.org>...
> > -----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$'
This would not match 'user at your.domain.com' .
>>> re.match(r'^.*?@\w?\.?domain\.com$','user at your.domain.com').group()
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
AttributeError: 'NoneType' object has no attribute 'group'
>>>
But all the ugly emails like e.g.:
>>> re.match(r'^.*?@\w?\.?domain\.com$','!"=?+#@b.domain.com').group()
'!"=?+#@b.domain.com'
I don't know the excact rules to perform a valid email,
but the above one doesn't seem valid to me.
>
> 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
I would prefer:
>>> re.match(r"^[\w.-]+@(sub\.)?domain.com$",'any at sub.domain.com').group()
'any at sub.domain.com'
>>> re.match(r"^[\w.-]+@(sub\.)?domain.com$",'any at domain.com').group()
'any at domain.com'
>>>
Though I'm not sure if
>>> re.match(r"^[\w.-]+@(sub\.)?domain.com$",'.-.-.- at domain.com').group()
'.-.-.- at domain.com'
>>>
is a valid email.
Peter
More information about the Python-list
mailing list