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

On Jul 20, 10:15 am, Luca S. [email protected] 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 :wink:

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

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

thnx a lot
LuCa

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

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs