[Tutor] Checking for valid e-mails

Charlie Clark charlie@begeistert.org
Thu Jan 30 11:39:02 2003


Dear list,

I have a bundle of e-mail addresses to check for validity and have come 
across a Python class which does this but not well enuff. I have found some 
perl code which is better but I'm having trouble translating some of the 
perl expressions.

The Python stuff is at
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66439/index_txt

It is a module with a single class which comes with some built in patterns 
and the ability to make custom patterns. The built in e-mail test doesn't 
catch things like spaces or @@ and other things.

The perl stuff is at
http://aspn.activestate.com/ASPN/Cookbook/Rx/Recipe/68432/index_txt

This seems to catch pretty much everything but it's perl and I'm not sure 
what !~ and =~ do

I've started work on making custom definitions based on the perl source like
this

sv1 = StringValidator("joe@testmail.com")
sv1.definePattern("test1", "^[0-9a-zA-Z\.\-\_]+\@[0-9a-zA-Z\.\-]+$")
sv1.definePattern("test2", "^[^0-9a-zA-Z]|[^0-9a-zA-Z]$")
if not sv1.isValidForPattern("test1"):
	print sv1.validateString, " has invalid characters in the name"
elif not sv1.isValidForPattern("test1"):
	print sv1.validateString, " doesn't start or end with alpha or num"
else:
	print sv1.validateString, "is valid"

Here test1 works as it should. I think !~ translates to matches. test2 
doesn't work here as it rejects pretty much all addresses. I'd expect it to 
work differently because it uses =~ but haven't quite worked out how.

I'll carry on experimenting! Thanx for any pointers.

Charlie