# Can't get my regexp to work to test if numerical

**URL:** <https://www.ruby-forum.com/t/cant-get-my-regexp-to-work-to-test-if-numerical/119016>\
**Category:** Ruby\
**Created:** [November 2, 2007, 5:47am UTC](https://www.ruby-forum.com/t/cant-get-my-regexp-to-work-to-test-if-numerical/119016 "2007-11-02T05:47:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pood](https://www.ruby-forum.com/u/pood)\
**Post date:** [November 2, 2007, 5:47am UTC](https://www.ruby-forum.com/t/cant-get-my-regexp-to-work-to-test-if-numerical/119016/1 "2007-11-02T05:47:58Z")

</div>

alright, this seems easy enough.

Building a Bank class, deposits must be floating, so if it has any  
letters, it will tell the user it needs numbers

```
     if amount == /^[0-9]$/
     deposit_to_acct(amount,current_account)
     puts 'You have deposited ' + amount.to_s

      else
      puts 'Must be a number, alphabet bank is next door to your

```

right’

```
    end

```

it goes straight to the else statement, never executes the  
deposit\_to\_acct method.

What part am I doing wrong?

---

<div class="post-metadata">

**Author:** ![pood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pood](https://www.ruby-forum.com/u/pood)\
**Post date:** [November 2, 2007, 6:05am UTC](https://www.ruby-forum.com/t/cant-get-my-regexp-to-work-to-test-if-numerical/119016/2 "2007-11-02T06:05:21Z")

</div>

Feng T. wrote:

> alright, this seems easy enough.
> 
> Building a Bank class, deposits must be floating, so if it has any  
> letters, it will tell the user it needs numbers
> 
> ```
> if amount == /^[0-9]$/
> deposit_to_acct(amount,current_account)
> puts 'You have deposited ' + amount.to_s
> 
> else
> puts 'Must be a number, alphabet bank is next door to your
> 
> ```
> 
> right’
> 
> ```
> end
> 
> ```

1. The pattern /^[0-9]$/ matches one digit.

2. amount would have to be a regex object to == the regex on the right.

if ‘abc’ == /abc/  
puts ‘yes’  
else  
puts ‘no’  
end

regex\_obj = /abc/

if regex\_obj == /abc/  
puts ‘yes’  
else  
puts ‘no’  
end

–output:–  
no  
yes

---

<div class="post-metadata">

**Author:** ![pood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pood](https://www.ruby-forum.com/u/pood)\
**Post date:** [November 2, 2007, 6:22am UTC](https://www.ruby-forum.com/t/cant-get-my-regexp-to-work-to-test-if-numerical/119016/3 "2007-11-02T06:22:35Z")

</div>

On Nov 2, 2007, at 12:47 AM, Feng T. wrote:

> right’
> 
> ```
> end
> 
> ```
> 
> it goes straight to the else statement, never executes the  
> deposit\_to\_acct method.
> 
> What part am I doing wrong?

I would say that you need a better understanding of regular  
expressions and how they are matched to strings in Ruby. The expression

```
amount == /^[0-9]$/

```

is an equality test and will always be false if ‘amount’ is a string.  
Consider the following examples:

`
# the following perform equality tests, not string to regular
expression matching
123 == /^[0-9]$/ # => false
'123' == /^[0-9]$/ # => false
/^[0-9]$/ == /^[0-9]$/ # => true
/^[0-9]$/.equal?(/^[0-9]$/ )# => false
# the following perform regular expression matching
'123' =~ /^[0-9]$/ # => nil (match fails because RE only accepts one
digit)
'3' =~ /^[0-9]$/ # => 0 (match succeeds)
'123' =~ /^[0-9]+$/ # => 0 (match succeeds)
'123X' =~ /^[0-9]+$/ # => nil (match fails)
`

Regards, Morton

---

<div class="post-metadata">

**Author:** ![pood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pood](https://www.ruby-forum.com/u/pood)\
**Post date:** [November 2, 2007, 6:33am UTC](https://www.ruby-forum.com/t/cant-get-my-regexp-to-work-to-test-if-numerical/119016/4 "2007-11-02T06:33:40Z")

</div>

7stud – wrote:

> Feng T. wrote:
> 
> > alright, this seems easy enough.
> > 
> > Building a Bank class, deposits must be floating, so if it has any  
> > letters, it will tell the user it needs numbers
> > 
> > ```
> > if amount == /^[0-9]$/
> > deposit_to_acct(amount,current_account)
> > puts 'You have deposited ' + amount.to_s
> > 
> > else
> > puts 'Must be a number, alphabet bank is next door to your
> > 
> > ```
> > 
> > right’
> > 
> > ```
> > end
> > 
> > ```
> 
> 1. The pattern /^[0-9]$/ matches one digit.

That’s incorrect. The pattern matches: the start of the line, followed  
by one digit, followed by the end of the line. So the regex won’t  
match a digit in a line containing two digits:

regex = /^[0-9]$/

line = ‘8’  
match\_obj = regex.match(line)  
p match\_obj.to\_a

line = ‘12’  
match\_obj = regex.match(line)  
p match\_obj.to\_a

–output:–  
[“8”]  
[]

You certainly don’t have to use regex’s to convert a string to a float  
(or an int). Look at String#to\_f and Kernel.Float.
