Email regex

Hi

I have a regular expression

/^([^.@\s,]+)@((?:[-a-z0-9]+.)+[a-z]{2,})$/i

This exactly fits my need except for one.

 But I have to pass this also   [email protected]      But Cannot.

Please help me to add this also to the above

Some example which are valid for above
[email protected]
[email protected]

Some example which are not valid for above
[email protected]
[email protected]
sijo@@yahoo.com

Thanks in advance
Sijo

[^.@\s,]

This character class excludes “.” “@” whitespace and “,” from being in
the first part of the e-mail.

Remove the dot and [email protected] will be allowed.

[^@\s,]

/^([^@\s,]+)@((?:[-a-z0-9]+.)+[a-z]{2,})$/i

However, [email protected] will also be allowed. It’s up to you here

  • do you want to make the regex perfect or will this be good enough.

I think what you want is a optional repeating sequence of characters
followed by exactly 1 “.” followed by a final sequence of characters
before the @

Something like this

/^((?:[^@\s,]+.)*[^@\s,]+)@((?:[-a-z0-9]+.)+[a-z]{2,})$/i

Maybe?

Though you probably need to read here:
http://www.regular-expressions.info/email.html
http://blog.krugle.com/?p=208

On Thu, Sep 24, 2009 at 10:48 AM, Sijo Kg [email protected] wrote:

Some example which are valid for above
Sijo

Posted via http://www.ruby-forum.com/.


Paul S.
http://www.nomadicfun.co.uk

[email protected]

On Thu, Sep 24, 2009 at 11:02 AM, Paul S. [email protected]
wrote:

However, [email protected] will also be allowed. It’s up to you here

  • do you want to make the regex perfect or will this be good enough.

I think what you want is a optional repeating sequence of characters
followed by exactly 1 “.” followed by a final sequence of characters
before the @

Something like this

/^((?:[^@\s,]+.)*[^@\s,]+)@((?:[-a-z0-9]+.)+[a-z]{2,})$/i

Hmm, i forgot to escape a dot in there…

/^((?:[^@\s,]+.)*[^@\s,]+)@((?:[-a-z0-9]+.)+[a-z]{2,})$/i

I have a regular expression


Paul S.
http://www.nomadicfun.co.uk

[email protected]

Hi Paul S.
Thanks again

Hmm, i forgot to escape a dot in there…

/^((?:[^@\s,]+.)*[^@\s,]+)@((?:[-a-z0-9]+.)+[a-z]{2,})$/i

 When tried this I get 2 failures

[email protected] and
[email protected]

Sijo

Did you read the pages I linked to?

/^((?:[^.@\s,]+.)*[^.@\s,]+)@((?:[-a-z0-9]+.)+[a-z]{2,})$/i

(I forgot to put the . back in the excluded character classes at the
start)

On Thu, Sep 24, 2009 at 11:22 AM, Sijo Kg [email protected] wrote:

[email protected]

Sijo


Posted via http://www.ruby-forum.com/.


Paul S.
http://www.nomadicfun.co.uk

[email protected]

Hi Paul S.

Did you read the pages I linked to?

/^((?:[^.@\s,]+.)*[^.@\s,]+)@((?:[-a-z0-9]+.)+[a-z]{2,})$/i

(I forgot to put the . back in the excluded character classes at the
start)

Lots of thanks.Now it works perfectly.Since it was a sudden fix I did
not .But now I start reading that pages to get a very good understanding
of regex

Thanks once again
Sijo

Hi Paul S.

Thanks for the reply  But if I use

/^((?:[^@\s,]+.)*[^@\s,]+)@((?:[-a-z0-9]+.)+[a-z]{2,})$/i

It fails
[email protected]
[email protected] and
sijo@[email protected]

Sijo

Sijo Kg:

I have a regular expression

/^([^.@\s,]+)@((?:[-a-z0-9]+.)+[a-z]{2,})$/i

This exactly fits my need except for one.

If your needs are checking email addresses’
validity, you really want to use something like¹

/^([a-zA-Z0-9&?/!|#*$^%=~{}+'-]+|"([\x00-\x0C\x0E-\x21\x23-\x5B\x5D -\x7F]|\\[\x00-\x7F])*")(\.([a-zA-Z0-9&_?\/!|#$^%=~{}+'-]+|"([\x00-
x0C\x0E-\x21\x23-\x5B\x5D-\x7F]|\[\x00-\x7F])
"))*@([a-zA-Z0-9&
?/! |#*$^%=~{}+'-]+|\[([\x00-\x0C\x0E-\x5A\x5E-\x7F]|\\[\x00-\x7F])*\])(\. ([a-zA-Z0-9&_?\/!|#$^%=~{}+'-]+|[([\x00-\x0C\x0E-\x5A\x5E-\x7F]|\[
\x00-\x7F])
]))*$/

…as
" spaces! @s! "escaped quotes!" "@yahoo.com
is a perfectly valid email address.

(Note that
" spaces! @s! "escaped quotes!" "@łódź.pl
is also a perfectly valid email address which the above doesn’t match.)

You might want to check
¹
http://blade.nagaokaut.ac.jp/cgi-bin/vframe.rb/ruby/ruby-talk/173923?173909-178505
for the discussion on this.

— Shot