filtering email addresses

L

Luca Scaljery

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
 
D

Dejan Dimic

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
 
L

Luca Scaljery

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
 
D

David A. Black

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

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
 
L

Luca Scaljery

David said:
Hi --



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
 
D

David A. Black

Hi --

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,661
Latest member
sxarexu

Latest Threads

Top