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.
67bb4df2775f6a6b603347dce7119571?d=identicon&s=25 unknown (Guest)
on 2006-06-01 12:38
(Received via mailing list)
In einer eMail vom 01.06.2006 12:21:49 Westeuropäische Normalzeit
schreibt
iiskakov@gmail.com:

How I  can simplify it:
params[:value] =~  /^\d+$|^-\d+$|^\d+\.\d+$|^-\d+\.\d+$/



OK, so, you want to extract the numbers that can by either floats or
integers from some
text.
You can use a * after something you don't necessarily need in your
Regexp,
and you can
use that on groups of characters, enclosed in brackets:



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.'
words=text.split
numbers=[]
words.each{|x|
if x=~  /^\-*\d+(\.\d+)*$/
numbers<<x
end
}
p 'all the numbers that  were there:'
p numbers

Best regards,

Axel
This topic is locked and can not be replied to.