# Test content string with regex

**URL:** <https://www.ruby-forum.com/t/test-content-string-with-regex/76513>\
**Category:** Ruby\
**Created:** [November 19, 2006, 10:20pm UTC](https://www.ruby-forum.com/t/test-content-string-with-regex/76513 "2006-11-19T22:20:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [November 19, 2006, 10:20pm UTC](https://www.ruby-forum.com/t/test-content-string-with-regex/76513/1 "2006-11-19T22:20:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [November 20, 2006, 9:19am UTC](https://www.ruby-forum.com/t/test-content-string-with-regex/76513/2 "2006-11-20T09:19:20Z")

</div>

Luca S. 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#String.scan](http://rubycentral.com/book/ref_c_string.html#String.scan) 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

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [November 20, 2006, 2:45pm UTC](https://www.ruby-forum.com/t/test-content-string-with-regex/76513/3 "2006-11-20T14:45:46Z")

</div>

“Luca S.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:removed\_email\_address@domain.invalid…

> p “NO”
> 
> –

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://jaredrichardson.net](http://jaredrichardson.net)

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [November 20, 2006, 2:46pm UTC](https://www.ruby-forum.com/t/test-content-string-with-regex/76513/4 "2006-11-20T14:46:24Z")

</div>

Luca S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

```

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [November 20, 2006, 2:45pm UTC](https://www.ruby-forum.com/t/test-content-string-with-regex/76513/5 "2006-11-20T14:45:50Z")

</div>

Alle 22:21, domenica 19 novembre 2006, Luca S. ha scritto:

> 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

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [November 21, 2006, 3:14am UTC](https://www.ruby-forum.com/t/test-content-string-with-regex/76513/6 "2006-11-21T03:14:38Z")

</div>

Bernard K. 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).

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [November 21, 2006, 5:21am UTC](https://www.ruby-forum.com/t/test-content-string-with-regex/76513/7 "2006-11-21T05:21:21Z")

</div>

Hi –

On Mon, 20 Nov 2006, Robert K. wrote:

> > else  
> > solely consists of digits this is what you would do:
> 
> c = “1234AA”
> 
> if /\A\d+\z/ =~ c

Or:

unless /\D/ =~ c

🙂

David

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [November 21, 2006, 1:32am UTC](https://www.ruby-forum.com/t/test-content-string-with-regex/76513/8 "2006-11-21T01:32:57Z")

</div>

----- Original Message -----  
From: “Jared R.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Newsgroups: comp.lang.ruby  
To: “ruby-talk ML” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Sunday, November 19, 2006 4:45 PM  
Subject: Re: test content string with regex

> > if c.scan( /1234567890/ )
> 
> c.scan is returning an empty array, not nil, so your ‘if’ statement  
> Jared  
> [http://jaredrichardson.net](http://jaredrichardson.net)
> 
> 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

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [November 21, 2006, 5:23am UTC](https://www.ruby-forum.com/t/test-content-string-with-regex/76513/9 "2006-11-21T05:23:22Z")

</div>

Hi –

On Tue, 21 Nov 2006, El Gato wrote:

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

I don’t either, but I’ve reluctantly had to accept that it’s faster  
than #match, at least in the comparisons I’ve seen. Of course that  
doesn’t matter unless it matters, so to speak.

David
