Filtering email addresses

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

···

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

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
--
Posted viahttp://www.ruby-forum.com/.

···

On Jul 20, 10:15 am, Luca Scaljery <lca...@gmail.com> wrote:
------------------------------------------------------------
String#scan
     str.scan(pattern) => array
     str.scan(pattern) {|match, ...| block } => str
------------------------------------------------------------------------

so if email.scan(/\@/) is always true :wink:

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

···

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

Hi --

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

str =~ /@/

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

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

···

On Sun, 20 Jul 2008, Luca Scaljery wrote:

--
Rails training from David A. Black and Ruby Power and Light:
   Intro to Ruby on Rails July 21-24 Edison, NJ
   Advancing With Rails August 18-21 Edison, NJ
See http://www.rubypal.com for details and updates!

David A. Black wrote:

Hi --

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 :slight_smile:

thnx a lot
LuCa

···

On Sun, 20 Jul 2008, Luca Scaljery wrote:

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

Hi --

···

On Sun, 20 Jul 2008, Luca Scaljery wrote:

David A. Black wrote:

Hi --

On Sun, 20 Jul 2008, Luca Scaljery wrote:

doesn't work

str =~ /@/

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

--
Rails training from David A. Black and Ruby Power and Light:
   Intro to Ruby on Rails July 21-24 Edison, NJ
   Advancing With Rails August 18-21 Edison, NJ
See http://www.rubypal.com for details and updates!

:slight_smile: you're right (I still have to get used to that!)

···

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