From: jason joo on
[Note: parts of this message were removed to make it a legal post.]

aha, i forgot positive number and dot. then the regular expression should
be /([\-+]?\d*[.]?\d*)/

eg

if input_string =~ /([\-+]?\d*[.]?\d*)/
input_string = $1
else
#have no number part
input_string = 0
end

9 => "9"
-9 => "-9"
-9.098 => "-9.098"

2010/8/11 Hassan Schroeder <hassan.schroeder(a)gmail.com>

> On Tue, Aug 10, 2010 at 6:38 PM, jason joo <jasonjoo.god(a)gmail.com> wrote:
> > or just
> >>> input_string.to_s.gsub(/\D/,'')
> > for number format permitted
>
> Depending on what your actual numeric use case is :-)
>
> $ irb
> >> input = 9
> => 9
> >> input.to_s.gsub(/\D/,'')
> => "9"
> >> input = 9.095
> => 9.095
> >> input.to_s.gsub(/\D/,'')
> => "9095"
> >>
>
> --
> Hassan Schroeder ------------------------ hassan.schroeder(a)gmail.com
> twitter: @hassan
>
>

From: Glenn Jackman on
At 2010-08-11 01:03AM, "jason joo" wrote:
> [Note: parts of this message were removed to make it a legal post.]
>
> aha, i forgot positive number and dot. then the regular expression should
> be /([\-+]?\d*[.]?\d*)/

The empty string matches that pattern (all your quantifiers allow zero
of the previous item). You want:

/[-+]?(?:\d+(\.\d*)?)|(?:\d*\.\d+)/

where there must be at least one digit.

--
Glenn Jackman
Write a wise saying and your name will live forever. -- Anonymous
First  |  Prev  | 
Pages: 1 2
Prev: New in classes
Next: Attributes in class