Forum: Ruby Re: How I can check number?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
unknown (Guest)
on 2006-06-01 14:19
(Received via mailing list)
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
This topic is locked and can not be replied to.