I need some help with a regexp please
Steve Holden
steve at holdenweb.com
Thu Sep 21 14:20:09 EDT 2006
codefire wrote:
> Hi,
>
> 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:
>
> [code]
> 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):
> addys.add(a)
>
> for a in sorted(addys):
> print a
> [/code]
>
> 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
> please?
>
The problem is that your pattern doesn't start out by confirming that
it's either at the start of a line or after whitespace. You could do
this with a "look-behind assertion" if you wanted.
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden
More information about the Python-list
mailing list