Perfect email match

Hello frnds

I want a code for .match for email…currently I am using
.match(/\S+@\S+.\S+/) but i want a better refined one if ne one has pls
let me know

thanks

dhaval parikh

On Mon, 3 Sep 2007, Dhaval P. wrote:

Hello frnds

I want a code for .match for email…currently I am using

No you don’t. You don’t want that at all :slight_smile:

.match(/\S+@\S+.\S+/) but i want a better refined one if ne one has pls
let me know

The later post on the same blog would suggest your pattern is
sufficient.
Give or take…
Hugh

Well thanks for the reply but can you tell me directly wat string should
i pass in .match condition…

thanks

Dhaval P. wrote:

Well thanks for the reply but can you tell me directly wat string should
i pass in .match condition…

thanks

.match(/\S+@\S+.\S+/) for matching email works fine but if the email
address is [email protected]. then it will also give the result as
[email protected]"." and i dont want that “.” to come in my string…so can
i trim it to return max of 3 chars while matching email id… is
something like that possible

thanks

dhaval parikh

2007/9/5, Dhaval P. [email protected]:

Well thanks for the reply but can you tell me directly wat string should
i pass in .match condition…

thanks

I can’t tell you the exact match, but I got the following from
http://www.cpan.org/authors/id/R/RJ/RJBS/Email-Valid-0.179.tar.gz in
the fileValid.pm:

$RFC822PAT = <<‘EOF’;
[\040\t](?:([^\\x80-\xff\n\015()](?:(?:\[^\x80-\xff]|([^\\x80-
xff\n\015()](?:\[^\x80-\xff][^\\x80-\xff\n\015()])))[^\\x80-\xf
f\n\015()]
))[\040\t])(?:(?:[^(\040)<>@,;:“.\[]\000-\037\x80-\x
ff]+(?![^(\040)<>@,;:”.\[]\000-\037\x80-\xff])|"[^\\x80-\xff\n\015
"]
(?:\[^\x80-\xff][^\\x80-\xff\n\015"])“)[\040\t](?:([^\\x80-
xff\n\015()]
(?:(?:\[^\x80-\xff]|([^\\x80-\xff\n\015()](?:\[^\x80
-\xff][^\\x80-\xff\n\015()]
)))[^\\x80-\xff\n\015()]))[\040\t]
)(?:.[\040\t](?:([^\\x80-\xff\n\015()](?:(?:\[^\x80-\xff]|([^
\x80-\xff\n\015()]
(?:\[^\x80-\xff][^\\x80-\xff\n\015()])))[^\
x80-\xff\n\015()]))[\040\t])(?:[^(\040)<>@,;:”.\[]\000-\037\x8
0-\xff]+(?![^(\040)<>@,;:“.\[]\000-\037\x80-\xff])|”[^\\x80-\xff\n
\015"](?:\[^\x80-\xff][^\\x80-\xff\n\015"])")[\040\t](?:([^\\x
80-\xff\n\015()](?:(?:\[^\x80-\xff]|([^\\x80-\xff\n\015()](?:\[^
\x80-\xff][^\\x80-\xff\n\015()])))[^\\x80-\xff\n\015()]))[\040
\t]))@[\040\t](?:([^\\x80-\xff\n\015()](?:(?:\[^\x80-\xff]|([
^\\x80-\xff\n\015()]
(?:\[^\x80-\xff][^\\x80-\xff\n\015()])))[^
\x80-\xff\n\015()]))[\040\t])(?:[^(\040)<>@,;:“.\[]\000-\037
x80-\xff]+(?![^(\040)<>@,;:”.\[]\000-\037\x80-\xff])|[(?:[^\\x80-
\xff\n\015[]]|\[^\x80-\xff])])[\040\t](?:([^\\x80-\xff\n\015()
](?:(?:\[^\x80-\xff]|([^\\x80-\xff\n\015()](?:\[^\x80-\xff][^\
x80-\xff\n\015()])))[^\\x80-\xff\n\015()]))[\040\t])(?:.[\04
0\t](?:([^\\x80-\xff\n\015()](?:(?:\[^\x80-\xff]|([^\\x80-\xff
n\015()](?:\[^\x80-\xff][^\\x80-\xff\n\015()])))[^\\x80-\xff\n
015()]
))[\040\t])(?:[^(\040)<>@,;:“.\[]\000-\037\x80-\xff]+(?!
[^(\040)<>@,;:”.\[]\000-\037\x80-\xff])|[(?:[^\\x80-\xff\n\015[
]]|\[^\x80-\xff])
])[\040\t](?:([^\\x80-\xff\n\015()](?:(?:\[^
x80-\xff]|([^\\x80-\xff\n\015()](?:\[^\x80-\xff][^\\x80-\xff\n\01
5()]
)))[^\\x80-\xff\n\015()]))[\040\t]))|(?:[^(\040)<>@,;:“.
\[]\000-\037\x80-\xff]+(?![^(\040)<>@,;:”.\[]\000-\037\x80-\xff]
)|“[^\\x80-\xff\n\015”](?:\[^\x80-\xff][^\\x80-\xff\n\015"])“)[^
()<>@,;:”.\[]\x80-\xff\000-\010\012-\037]
(?:(?:([^\\x80-\xff\n\0
15()](?:(?:\[^\x80-\xff]|([^\\x80-\xff\n\015()](?:\[^\x80-\xff][
^\\x80-\xff\n\015()])))[^\\x80-\xff\n\015()]))|“[^\\x80-\xff
n\015”](?:\[^\x80-\xff][^\\x80-\xff\n\015"])“)[^()<>@,;:”.\[]
x80-\xff\000-\010\012-\037]
)<[\040\t](?:([^\\x80-\xff\n\015()](?
:(?:\[^\x80-\xff]|([^\\x80-\xff\n\015()]
(?:\[^\x80-\xff][^\\x80-
\xff\n\015()])))[^\\x80-\xff\n\015()]))[\040\t])(?:@[\040\t]*
(?:([^\\x80-\xff\n\015()](?:(?:\[^\x80-\xff]|([^\\x80-\xff\n\015
()]
(?:\[^\x80-\xff][^\\x80-\xff\n\015()])))[^\\x80-\xff\n\015()
]))[\040\t])(?:[^(\040)<>@,;:“.\[]\000-\037\x80-\xff]+(?![^(\0
40)<>@,;:”.\[]\000-\037\x80-\xff])|[(?:[^\\x80-\xff\n\015[]]|\
[^\x80-\xff])])[\040\t](?:([^\\x80-\xff\n\015()](?:(?:\[^\x80-
xff]|([^\\x80-\xff\n\015()]
(?:\[^\x80-\xff][^\\x80-\xff\n\015()]*
)))[^\\x80-\xff\n\015()]))[\040\t])(?:.[\040\t](?:([^\\x80
-\xff\n\015()](?:(?:\[^\x80-\xff]|([^\\x80-\xff\n\015()](?:\[^\x
80-\xff][^\\x80-\xff\n\015()])))[^\\x80-\xff\n\015()]))[\040\t
])(?:[^(\040)<>@,;:“.\[]\000-\037\x80-\xff]+(?![^(\040)<>@,;:”.\
[]\000-\037\x80-\xff])|[(?:[^\\x80-\xff\n\015[]]|\[^\x80-\xff])
])[\040\t](?:([^\\x80-\xff\n\015()](?:(?:\[^\x80-\xff]|([^\\x
80-\xff\n\015()]
(?:\[^\x80-\xff][^\\x80-\xff\n\015()])))[^\\x80
-\xff\n\015()]))[\040\t]))(?:,[\040\t](?:([^\\x80-\xff\n\015(
)](?:(?:\[^\x80-\xff]|([^\\x80-\xff\n\015()](?:\[^\x80-\xff][^\
\x80-\xff\n\015()])))[^\\x80-\xff\n\015()]))[\040\t])@[\040\t
](?:([^\\x80-\xff\n\015()](?:(?:\[^\x80-\xff]|([^\\x80-\xff\n\0
15()](?:\[^\x80-\xff][^\\x80-\xff\n\015()])))[^\\x80-\xff\n\015
()]
))[\040\t])(?:[^(\040)<>@,;:“.\[]\000-\037\x80-\xff]+(?![^(
\040)<>@,;:”.\[]\000-\037\x80-\xff])|[(?:[^\\x80-\xff\n\015[]]|
\[^\x80-\xff])
])[\040\t](?:([^\\x80-\xff\n\015()](?:(?:\[^\x80
-\xff]|([^\\x80-\xff\n\015()](?:\[^\x80-\xff][^\\x80-\xff\n\015()
]
)))[^\\x80-\xff\n\015()]))[\040\t])(?:.[\040\t](?:([^\\x
80-\xff\n\015()](?:(?:\[^\x80-\xff]|([^\\x80-\xff\n\015()](?:\[^
\x80-\xff][^\\x80-\xff\n\015()])))[^\\x80-\xff\n\015()]))[\040
\t])(?:[^(\040)<>@,;:“.\[]\000-\037\x80-\xff]+(?![^(\040)<>@,;:”.
\[]\000-\037\x80-\xff])|[(?:[^\\x80-\xff\n\015[]]|\[^\x80-\xff
])])[\040\t](?:([^\\x80-\xff\n\015()](?:(?:\[^\x80-\xff]|([^\
\x80-\xff\n\015()]
(?:\[^\x80-\xff][^\\x80-\xff\n\015()])))[^\\x
80-\xff\n\015()]))[\040\t]))):[\040\t](?:([^\\x80-\xff\n\015
()]
(?:(?:\[^\x80-\xff]|([^\\x80-\xff\n\015()](?:\[^\x80-\xff][^
\x80-\xff\n\015()]
)))[^\\x80-\xff\n\015()]))[\040\t]))?(?:[^
(\040)<>@,;:“.\[]\000-\037\x80-\xff]+(?![^(\040)<>@,;:”.\[]\000-
\037\x80-\xff])|“[^\\x80-\xff\n\015”]
(?:\[^\x80-\xff][^\\x80-\xff
n\015"])“)[\040\t](?:([^\\x80-\xff\n\015()](?:(?:\[^\x80-\xff]|
([^\\x80-\xff\n\015()](?:\[^\x80-\xff][^\\x80-\xff\n\015()])))
[^\\x80-\xff\n\015()]
))[\040\t])(?:.[\040\t](?:([^\\x80-\xff
\n\015()](?:(?:\[^\x80-\xff]|([^\\x80-\xff\n\015()](?:\[^\x80-\x
ff][^\\x80-\xff\n\015()])))[^\\x80-\xff\n\015()]))[\040\t])(
?:[^(\040)<>@,;:”.\[]\000-\037\x80-\xff]+(?![^(\040)<>@,;:“.\[]
000-\037\x80-\xff])|”[^\\x80-\xff\n\015"](?:\[^\x80-\xff][^\\x80-
xff\n\015"]
)")[\040\t](?:([^\\x80-\xff\n\015()](?:(?:\[^\x80-\x
ff]|([^\\x80-\xff\n\015()]
(?:\[^\x80-\xff][^\\x80-\xff\n\015()])
))[^\\x80-\xff\n\015()])
)[\040\t]))@[\040\t](?:([^\\x80-\x
ff\n\015()](?:(?:\[^\x80-\xff]|([^\\x80-\xff\n\015()](?:\[^\x80-
\xff][^\\x80-\xff\n\015()])))[^\\x80-\xff\n\015()]))[\040\t])
(?:[^(\040)<>@,;:“.\[]\000-\037\x80-\xff]+(?![^(\040)<>@,;:”.\[
]\000-\037\x80-\xff])|[(?:[^\\x80-\xff\n\015[]]|\[^\x80-\xff])
]
)[\040\t]
(?:([^\\x80-\xff\n\015()](?:(?:\[^\x80-\xff]|([^\\x80-
\xff\n\015()]
(?:\[^\x80-\xff][^\\x80-\xff\n\015()])))[^\\x80-\x
ff\n\015()]))[\040\t])(?:.[\040\t](?:([^\\x80-\xff\n\015()](
?:(?:\[^\x80-\xff]|([^\\x80-\xff\n\015()](?:\[^\x80-\xff][^\\x80
-\xff\n\015()]
)))[^\\x80-\xff\n\015()]))[\040\t])*(?:[^(\040)<

@,;:“.\[]\000-\037\x80-\xff]+(?![^(\040)<>@,;:”.\[]\000-\037\x8
0-\xff])|[(?:[^\\x80-\xff\n\015[]]|\[^\x80-\xff])])[\040\t](?:
([^\\x80-\xff\n\015()](?:(?:\[^\x80-\xff]|([^\\x80-\xff\n\015()]
(?:\[^\x80-\xff][^\\x80-\xff\n\015()])
))[^\\x80-\xff\n\015()])
)[\040\t])
)*>)
EOF

This seems to be the complete RegEx for email addresses following
RFC822, but I had no time to translate it for Ruby and to test it.
Please mail here if you change it in Ruby.

There’s also validates_as_email. Last time I checked the repository over
at Greenpeace (which hosts it) was down, but there are myriad projects
out there that will allow you to check out validates_as_email from their
repositories.

Note that “perfect matching” and RFC822 compliance don’t always match up
– RFC822 allows addresses like user@domain without a top-level domain
for internal e-mail, though they are likely completely besides the point
in most Rails applications.


Roderick van Domburg