# Filtering email addresses

**URL:** <https://www.ruby-forum.com/t/filtering-email-addresses/143547>\
**Category:** Ruby\
**Created:** [July 20, 2008, 10:20am UTC](https://www.ruby-forum.com/t/filtering-email-addresses/143547 "2008-07-20T10:20:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [July 20, 2008, 10:20am UTC](https://www.ruby-forum.com/t/filtering-email-addresses/143547/1 "2008-07-20T10:20:36Z")

</div>

Hi All

I’m trying to filter email adresses like this

#! /usr/bin/ruby

email = “test\_test.test”

if email.scan(/@/)  
p “yes”  
end

For some reason my program always prints ‘yes’

How would should I do this ?

thnx  
LuCa

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [July 20, 2008, 11:17am UTC](https://www.ruby-forum.com/t/filtering-email-addresses/143547/2 "2008-07-20T11:17:14Z")

</div>

On Jul 20, 10:15 am, Luca S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> For some reason my program always prints ‘yes’
> 
> How would should I do this ?
> 
> ## thnx LuCa
> 
> Posted viahttp://www.ruby-forum.com/.

* * *

## String#scan str.scan(pattern) =\> array str.scan(pattern) {|match, …| block } =\> str

so if email.scan(/@/) is always true 😉

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [July 20, 2008, 1:49pm UTC](https://www.ruby-forum.com/t/filtering-email-addresses/143547/3 "2008-07-20T13:49:58Z")

</div>

Hi –

On Sun, 20 Jul 2008, Luca S. wrote:

> doesn’t work

str =~ /@/

It’s absolutely not a robust way to check for an email address, but it  
will check for an at-sign 🙂

As for /[^pattern]/, that will match one character that is not a, e,  
n, p, t, or r. For non-matching, try:

str !~ /pattern/

or maybe negative assertions in the regex.

David

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [July 20, 2008, 1:58pm UTC](https://www.ruby-forum.com/t/filtering-email-addresses/143547/4 "2008-07-20T13:58:22Z")

</div>

David A. Black wrote:

> Hi –
> 
> On Sun, 20 Jul 2008, Luca S. wrote:
> 
> > doesn’t work
> 
> str =~ /@/

thnx, I was looking for a String#method but this is OK too

> As for /[^pattern]/, that will match one character that is not a, e,  
> n, p, t, or r. For non-matching, try:

my mistake 🙂

thnx a lot  
LuCa

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [July 20, 2008, 2:04pm UTC](https://www.ruby-forum.com/t/filtering-email-addresses/143547/5 "2008-07-20T14:04:01Z")

</div>

Hi –

On Sun, 20 Jul 2008, Luca S. wrote:

> thnx, I was looking for a String#method but this is OK too  
> =~ is a String method.

* * *

## String#=~ str =~ obj =\> fixnum or nil

```
  Match---If _obj_ is a +Regexp+, use it as a pattern to match
  against _str_,and returns the position the match starts, or +nil+
  if there is no match. Otherwise, invokes _obj.=~_, passing _str_
  as an argument. The default +=~+ in +Object+ returns +false+.

     "cat o' 9 tails" =~ /\d/ #=> 7
     "cat o' 9 tails" =~ 9 #=> false

```

David

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [July 20, 2008, 2:06pm UTC](https://www.ruby-forum.com/t/filtering-email-addresses/143547/6 "2008-07-20T14:06:45Z")

</div>

🙂 you’re right (I still have to get used to that!)

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [July 20, 2008, 12:12pm UTC](https://www.ruby-forum.com/t/filtering-email-addresses/143547/7 "2008-07-20T12:12:08Z")

</div>

thnx, makes sense

How do you, BTW, exlude matches ?

!str.scan(pattern) do  
…  
end

doesn’t work. Also a regex like  
/[^pattern]/

doesn’t work
