[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

test content string with regex

Luca Scaljery

11/19/2006 9:21:00 PM

Hi All

I just tried to test the content of a string, something like this:


c = "1234AA"

if c.scan( /1234567890/ )
p "OK"
else
p "NO"
end


This always returns "OK" :(

Any suggestions what goes wrong here. And is this the way to check the
content of a string ?

Thanks a lot
LuCa

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

9 Answers

Jared Richardson

11/19/2006 9:41:00 PM

0


"Luca Scaljery" <lcalje@gmail.com> wrote in message
news:30fcaa110523581c1bf28e6b984118ba@ruby-forum.com...
> Hi All
>
> I just tried to test the content of a string, something like this:
>
>
> c = "1234AA"
>
> if c.scan( /1234567890/ )
> p "OK"
> else
> p "NO"
> end
>
>
> This always returns "OK" :(
>
> Any suggestions what goes wrong here. And is this the way to check the
> content of a string ?
>
> Thanks a lot
> LuCa
>
> --

because of this

irb(main):008:0> a=c.scan( /1234567890/ )
=> []

c.scan is returning an empty array, not nil, so your 'if' statement passes.

I prefer to do a regex for what you seem to be trying to do.

if (c =~(/1234567890/ )) then

or if((index=c=~/23211/)) then

=~ returns nil for no match. Otherwise you get the index of the content.

Jared
http://jaredrich...



Stefano Crocco

11/19/2006 10:17:00 PM

0

Alle 22:21, domenica 19 novembre 2006, Luca Scaljery ha scritto:
> Hi All
>
> I just tried to test the content of a string, something like this:
>
>
> c = "1234AA"
>
> if c.scan( /1234567890/ )
> p "OK"
> else
> p "NO"
> end
>
>
> This always returns "OK" :(
>
> Any suggestions what goes wrong here. And is this the way to check the
> content of a string ?
>
> Thanks a lot
> LuCa

Even when there's no match, String#scan returns an array, so your is always
true. To make it work, you should use
if !c.scan(/1234567890/).empty?

As for your second question, it depends on what exactly you want to do. It is
the correct form if you need to get all the numbers in the string, regardless
of their position or any other thing (by the way, if you want to match a
digit in a regexp, you can use /\d/). If you simply want to check whether the
string contains a digit, you can use String#match or =~ with the /\d/ regexp.
If you need more control on the scanning, you can use the StringScanner
class.

I hope this helps

Stefano

Robert Klemme

11/20/2006 8:13:00 AM

0

Luca Scaljery <lcalje@gmail.com> wrote:
> Hi All
>
> I just tried to test the content of a string, something like this:
>
>
> c = "1234AA"
>
> if c.scan( /1234567890/ )
> p "OK"
> else
> p "NO"
> end
>
>
> This always returns "OK" :(
>
> Any suggestions what goes wrong here. And is this the way to check the
> content of a string ?

What exactly do you want to check? If you want to make sure that a string
solely consists of digits this is what you would do:

c = "1234AA"

if /\A\d+\z/ =~ c
puts "ok"
else
puts "nok"
end

Kind regards

robert

Shai Rosenfeld

11/20/2006 8:19:00 AM

0

Luca Scaljery wrote:
> Hi All
>
> I just tried to test the content of a string, something like this:
>
>
> c = "1234AA"
>
> if c.scan( /1234567890/ )
> p "OK"
> else
> p "NO"
> end
>
>
> This always returns "OK" :(
>
> Any suggestions what goes wrong here. And is this the way to check the
> content of a string ?
>
> Thanks a lot
> LuCa

(i think) the reason you always get OK, is because the if c.scan always
returns a true value (the scan method always takes place) (i.e, it never
returns nil/false)...i don't know exactly what method you need, but
first of all check out
http://rubycentral.com/book/ref_c_string.html#S... to see that it
returns either an array or a string, and second of all maybe, check out,
Regexp#=~ , maybe that is better?

hth,
harp



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

Bernard Kenik

11/21/2006 12:31:00 AM

0


----- Original Message -----
From: "Jared Richardson" <jared-richRMV-THSardson@nc.rr.com>
Newsgroups: comp.lang.ruby
To: "ruby-talk ML" <ruby-talk@ruby-lang.org>
Sent: Sunday, November 19, 2006 4:45 PM
Subject: Re: test content string with regex


>
> "Luca Scaljery" <lcalje@gmail.com> wrote in message
> news:30fcaa110523581c1bf28e6b984118ba@ruby-forum.com...
>> Hi All
>>
>> I just tried to test the content of a string, something like this:
>>
>>
>> c = "1234AA"
>>
>> if c.scan( /1234567890/ )
>> p "OK"
>> else
>> p "NO"
>> end
>>
>>
>> This always returns "OK" :(
>>
>> Any suggestions what goes wrong here. And is this the way to check the
>> content of a string ?
>>
>> Thanks a lot
>> LuCa
>>
>> --
>
> because of this
>
> irb(main):008:0> a=c.scan( /1234567890/ )
> => []
>
> c.scan is returning an empty array, not nil, so your 'if' statement
> passes.
>
> I prefer to do a regex for what you seem to be trying to do.
>
> if (c =~(/1234567890/ )) then
>
> or if((index=c=~/23211/)) then
>
> =~ returns nil for no match. Otherwise you get the index of the content.
>
> Jared
> http://jaredrich...
>
>
>
>
>the OP asked why it was always true

this is a ruby gotcha

the only thing that returns false or false or nil

0 , '0', '''', an empty array, or an empty hash all evaluate to true


wmwilson01

11/21/2006 2:15:00 AM

0

Bernard Kenik wrote:
>
> this is a ruby gotcha
>
> the only thing that returns false or false or nil
>
> 0 , '0', '''', an empty array, or an empty hash all evaluate to true

Saying it's a "gotcha" makes it sound like the behavior is wrong. Some
would say that only false should be false. Me, I like that nil is
false, but I sure as hell don't want 0 or '' or [] or {} to be false.
It behaves well -- that's not a gotcha -- that's just good common sense.

I can't quite tell what you're looking for exactly, but String#match is
probably it. (I don't like the =~ perlism myself).


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

dblack

11/21/2006 4:19:00 AM

0

dblack

11/21/2006 4:22:00 AM

0

Dawlish

9/27/2010 3:43:00 PM

0

On Sep 27, 4:24 pm, Desertphile <desertph...@invalid-address.net>
wrote:
> On Sun, 26 Sep 2010 23:55:42 -0700 (PDT), Dawlish
>
> <pjg...@hotmail.com> wrote:
> >>> Are you saying that the volcanic activity has increased in the past
> >>> 150 years? Just a "yes" or "no" will be fine.
>
> The correct answer, of course, is "No." But the alarmist nutcase
> will never answer the question because he knows he lied.
>
> > On Sep 27, 1:41 am, Last Post <last_p...@primus.ca> wrote...
>
> ... nothing true.
>
> > A typical denier's obfuscation. You ask them a question, they wriggle
> > and squirm but won't answer, because by actually answering, they'll
> > look an idiot and show themselves up. Some of them also try to hide
> > their answers. (sorry if you did answer and I missed it in your sig).
> > It's a simple enough question. Answer it clearly.
>
> Meanwhile, the tiny rise in deep-water temperature came as a
> surprise to climatologists and oceanographers. It was unexpected.
> We can still hope it is instrument error.
>
> --http://deser...
> Desertphile's Desert Soliloquy. WARNING: view with plenty of water
> "Why aren't resurrections from the dead noteworthy?" -- Jim Rutz

We should hope it is not an actual rise. No-one would want that.