# Email regex

**URL:** <https://www.ruby-forum.com/t/email-regex/176073>\
**Category:** Ruby\
**Created:** [September 24, 2009, 11:47am UTC](https://www.ruby-forum.com/t/email-regex/176073 "2009-09-24T11:47:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [September 24, 2009, 11:47am UTC](https://www.ruby-forum.com/t/email-regex/176073/1 "2009-09-24T11:47:53Z")

</div>

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 removed_email_address@domain.invalid But Cannot.

```

Please help me to add this also to the above

Some example which are valid for above  
removed\_email\_address@domain.invalid  
removed\_email\_address@domain.invalid

Some example which are not valid for above  
removed\_email\_address@domain.invalid  
removed\_email\_address@domain.invalid  
sijo@@yahoo.com

Thanks in advance  
Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [September 24, 2009, 12:03pm UTC](https://www.ruby-forum.com/t/email-regex/176073/2 "2009-09-24T12:03:26Z")

</div>

[^.@\s,]

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

Remove the dot and removed\_email\_address@domain.invalid will be allowed.

[^@\s,]

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

However, removed\_email\_address@domain.invalid 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://www.regular-expressions.info/email.html)  
[http://blog.krugle.com/?p=208](http://blog.krugle.com/?p=208)

On Thu, Sep 24, 2009 at 10:48 AM, Sijo Kg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## Some example which are valid for above Sijo
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

–  
Paul S.  
[http://www.nomadicfun.co.uk](http://www.nomadicfun.co.uk)

removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [September 24, 2009, 12:13pm UTC](https://www.ruby-forum.com/t/email-regex/176073/3 "2009-09-24T12:13:58Z")

</div>

On Thu, Sep 24, 2009 at 11:02 AM, Paul S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> However, removed\_email\_address@domain.invalid 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](http://www.nomadicfun.co.uk)

removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [September 24, 2009, 12:22pm UTC](https://www.ruby-forum.com/t/email-regex/176073/4 "2009-09-24T12:22:19Z")

</div>

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

```

removed\_email\_address@domain.invalid and  
removed\_email\_address@domain.invalid

Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [September 24, 2009, 12:30pm UTC](https://www.ruby-forum.com/t/email-regex/176073/5 "2009-09-24T12:30:22Z")

</div>

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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> removed\_email\_address@domain.invalid
> 
> Sijo
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

–  
Paul S.  
[http://www.nomadicfun.co.uk](http://www.nomadicfun.co.uk)

removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [September 24, 2009, 12:44pm UTC](https://www.ruby-forum.com/t/email-regex/176073/6 "2009-09-24T12:44:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [September 24, 2009, 12:15pm UTC](https://www.ruby-forum.com/t/email-regex/176073/7 "2009-09-24T12:15:55Z")

</div>

Hi Paul S.

```
Thanks for the reply But if I use

```

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

It fails  
removed\_email\_address@domain.invalid  
removed\_email\_address@domain.invalid and  
sijo@removed\_email\_address@domain.invalid

Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [September 24, 2009, 5:36pm UTC](https://www.ruby-forum.com/t/email-regex/176073/8 "2009-09-24T17:36:00Z")

</div>

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](http://blade.nagaokaut.ac.jp/cgi-bin/vframe.rb/ruby/ruby-talk/173923?173909-178505)  
for the discussion on this.

â€” Shot
