[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

filtering email addresses

Luca Scaljery

7/20/2008 8:15:00 AM

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

6 Answers

Dejan Dimic

7/20/2008 9:11:00 AM

0

On Jul 20, 10:15 am, Luca Scaljery <lca...@gmail.com> wrote:
> 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-....


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

so if email.scan(/\@/) is always true ;-)

Luca Scaljery

7/20/2008 10:07:00 AM

0

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

David A. Black

7/20/2008 11:44:00 AM

0

Hi --

On Sun, 20 Jul 2008, Luca Scaljery wrote:

> 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

--
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.r... for details and updates!

Luca Scaljery

7/20/2008 11:53:00 AM

0

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

>
> 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
--
Posted via http://www.ruby-....

David A. Black

7/20/2008 11:58:00 AM

0

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.r... for details and updates!

Luca Scaljery

7/20/2008 12:01:00 PM

0

:) you're right (I still have to get used to that!)
--
Posted via http://www.ruby-....