Re: How I can check number?


#1

How I can to check float value, with regular expression. Also need
check
a integer with float values.

I do not know exactly what you mean by this:
do you want to find a string that could represent a float (resp.
integer)
number
in a text ?
That could be a string made up of

  • an arbitrary number of 0,1,…,9 , followed by a dot, followed by an
    arbitrary number
    of 0,1,2,…,9 in case of a float number,
  • an arbitrary number of 0,1,…,9 in case of an integer number.

Then, you could do this

class String
def scan_for_numbers
int_regexp=/+-[0-9]+/
fl_regexp=/+-[0-9]+.[0-9]+/

     temp=self.dup

floats=temp.scan(fl_regexp)
floats.each{|x| temp.gsub!(x,’’)}
ints=temp.scan(int_regexp)
all_numbers={}
floats.each{|x| all_numbers.update({x,’ is a floating point
number!’})}
ints.each{|x| all_numbers.update({x,’ is an integer number!’})}
return all_numbers
end
end

a_long_text=‘my bank account is -100000000.09 dollars, but I earn 1
dollar a
day, which is better than earning 0.50 dollars or 0 dollars.’

b=a_long_text.scan_for_numbers
puts b


gives:
-100000000.09 is a floating point number!0 is an integer number!1 is an
integer number!0.50 is a floating point number!

Best regards,

Axel