I need some help with a regexp please

codefire tony.bedford at gmail.com
Thu Sep 21 13:14:42 EDT 2006


I am trying to get a regexp to validate email addresses but can't get
it quite right. The problem is I can't quite find the regexp to deal
with ignoring the case james..kirk at fred.com, which is not valid. Here's
my attempt, neither of my regexps work quite how I want:

import os
import re

s = 'Hi james..kirk at fred.com dr.spock at blarg.com jim at home.com @@not
scottie at home.space.com partridge in a pear tree'
r = re.compile(r'\w+\.?\w+@[^@\s]+\.\w+')
#r = re.compile(r'[a-z\-\.]+@[a-z\-\.]+')

addys = set()
for a in r.findall(s):

for a in sorted(addys):
    print a

This gives:
dr.spock at blarg.com
jim at home.com
kirk at fred.com   <-- shouldn't be here :(
scottie at home.space.com

Nearly there but no cigar :)

I can't see the wood for the trees now :) Can anyone suggest a fix


More information about the Python-list mailing list