# Perfect email match

**URL:** <https://www.ruby-forum.com/t/perfect-email-match/113661>\
**Category:** Rails\
**Created:** [September 3, 2007, 4:34pm UTC](https://www.ruby-forum.com/t/perfect-email-match/113661 "2007-09-03T16:34:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dhaval\_parikh33](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@dhaval\_parikh33](https://www.ruby-forum.com/u/dhaval_parikh33)\
**Post date:** [September 3, 2007, 4:34pm UTC](https://www.ruby-forum.com/t/perfect-email-match/113661/1 "2007-09-03T16:34:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dhaval\_parikh33](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@dhaval\_parikh33](https://www.ruby-forum.com/u/dhaval_parikh33)\
**Post date:** [September 3, 2007, 5:10pm UTC](https://www.ruby-forum.com/t/perfect-email-match/113661/2 "2007-09-03T17:10:59Z")

</div>

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 🙂

> **[I Knew How To Validate An Email Address Until I Read The RFC](https://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx/)**
>
> Raise your hand if you know how to validate an email address. For those of you with your hand in the air, put it down quickly before someone sees you. It’s an odd sight to see someone sitting alone at the keyboard raising his or her hand. I was...

> .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

---

<div class="post-metadata">

**Author:** ![dhaval\_parikh33](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@dhaval\_parikh33](https://www.ruby-forum.com/u/dhaval_parikh33)\
**Post date:** [September 5, 2007, 10:52am UTC](https://www.ruby-forum.com/t/perfect-email-match/113661/3 "2007-09-05T10:52:26Z")

</div>

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

thanks

---

<div class="post-metadata">

**Author:** ![dhaval\_parikh33](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@dhaval\_parikh33](https://www.ruby-forum.com/u/dhaval_parikh33)\
**Post date:** [September 5, 2007, 11:28am UTC](https://www.ruby-forum.com/t/perfect-email-match/113661/4 "2007-09-05T11:28:04Z")

</div>

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 removed\_email\_address@domain.invalid. then it will also give the result as  
removed\_email\_address@domain.invalid"." 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

---

<div class="post-metadata">

**Author:** ![dhaval\_parikh33](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@dhaval\_parikh33](https://www.ruby-forum.com/u/dhaval_parikh33)\
**Post date:** [September 5, 2007, 11:48am UTC](https://www.ruby-forum.com/t/perfect-email-match/113661/5 "2007-09-05T11:48:50Z")

</div>

2007/9/5, Dhaval P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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](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.

---

<div class="post-metadata">

**Author:** ![dhaval\_parikh33](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@dhaval\_parikh33](https://www.ruby-forum.com/u/dhaval_parikh33)\
**Post date:** [September 5, 2007, 5:30pm UTC](https://www.ruby-forum.com/t/perfect-email-match/113661/6 "2007-09-05T17:30:28Z")

</div>

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

> **[De domeinnaam Nedforce.nl is te koop.](http://www.nedforce.nl)**
>
> Deze mooie domeinnaam kan al over een paar minuten van jou zijn. Betalen gaat eenvoudig en vertrouwd via iDEAL. Direct na de betaling ontvang je de verhuiscode per e-mail en kun je aan de slag.
