[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Scanning a string for decimal numbers

Jeppe Jakobsen

2/4/2006 10:21:00 PM

Hi all, how do you scan a string and avoid getting my decimal numbers
divided into 2 numbers.

Example:

a = "24,4 + 55,2"
a.scan! (/\d+/)
puts a

my output for a will be:
24
4
55
2

But I want to keep my decimal numbers intact like this:
24,4
55,2


How do I solve this problem without putting the numbers into seperate
strings?
20 Answers

Kent Sibilev

2/4/2006 10:37:00 PM

0

"24,4 + 55,2".scan /[\d,]+/

Kent

On 2/4/06, Jeppe Jakobsen <jeppe88@gmail.com> wrote:
> Hi all, how do you scan a string and avoid getting my decimal numbers
> divided into 2 numbers.
>
> Example:
>
> a = "24,4 + 55,2"
> a.scan! (/\d+/)
> puts a
>
> my output for a will be:
> 24
> 4
> 55
> 2
>
> But I want to keep my decimal numbers intact like this:
> 24,4
> 55,2
>
>
> How do I solve this problem without putting the numbers into seperate
> strings?
>
>


Ernest Ellingson

2/4/2006 11:16:00 PM

0

Kent Sibilev wrote:
> "24,4 + 55,2".scan /[\d,]+/
>
> Kent
>
> On 2/4/06, Jeppe Jakobsen <jeppe88@gmail.com> wrote:
>> Hi all, how do you scan a string and avoid getting my decimal numbers
>> divided into 2 numbers.
>>
>> Example:
>>
>> a = "24,4 + 55,2"
>> a.scan! (/\d+/)
>> puts a
>>
>> my output for a will be:
>> 24
>> 4
>> 55
>> 2
>>
>> But I want to keep my decimal numbers intact like this:
>> 24,4
>> 55,2
>>
>>
>> How do I solve this problem without putting the numbers into seperate
>> strings?
>>
>>
>
>
try
a.scan!(/\d+,\d+/)
Ernie

--
"Keep an open mind, but not so open that your brain falls out." (Richard
Feynman)

Ara.T.Howard

2/5/2006 2:48:00 AM

0

Antonio Cangiano

2/5/2006 11:44:00 AM

0

Jeppe Jakobsen wrote:
> Hi all, how do you scan a string and avoid getting my decimal numbers
> divided into 2 numbers.
>
> Example:
>
> a = "24,4 + 55,2"
> a.scan! (/\d+/)
> puts a
>
> my output for a will be:
> 24
> 4
> 55
> 2
>
> But I want to keep my decimal numbers intact like this:
> 24,4
> 55,2
>
>
> How do I solve this problem without putting the numbers into seperate
> strings?

Hi Jeppe,
you can use the following:

a.scan /[-+]?[0-9]*\,?[0-9]+/

which will take care of negative numbers as well.

HTH,
Antonio
--
Zen and the Art of Ruby Programming
http://www.antonioca...

Antonio Cangiano

2/5/2006 12:01:00 PM

0

> a.scan /[-+]?[0-9]*\,?[0-9]+/

Or to be less verbose you can use \d in place of [0-9] :-)

HTH
Antonio
--
Zen and the Art of Ruby Programming
http://www.antonioca...

Josef 'Jupp' Schugt

2/11/2006 10:21:00 PM

0

Hi!

At Sun, 05 Feb 2006 11:43:52 +0000, Antonio Cangiano wrote:
> a.scan /[-+]?[0-9]*\,?[0-9]+/

Shouldn't that rather be the following?

a.scan /[-+]?([1-9]\d*(\,[0-9]+)?)|(0(\,[0-9]+)?)/

Josef 'Jupp' Schugt
--
Let the origin be the middle of the earth, p(x,r) be the probability
density for finding person x at distance r. Make sure that a permanent
solution of int_0^R p(x,r) dr < 1 exists for R being the instantanous
value of the distance between earth and mars.

Jeppe Jakobsen

2/11/2006 11:13:00 PM

0

Will that expression include both integers and decimal numbers?

Wilson Bilkovich

2/11/2006 11:30:00 PM

0

On 2/4/06, Jeppe Jakobsen <jeppe88@gmail.com> wrote:
> Hi all, how do you scan a string and avoid getting my decimal numbers
> divided into 2 numbers.
>
> Example:
>
> a = "24,4 + 55,2"
> a.scan! (/\d+/)
> puts a
>
> my output for a will be:
> 24
> 4
> 55
> 2
>
> But I want to keep my decimal numbers intact like this:
> 24,4
> 55,2
>
>
> How do I solve this problem without putting the numbers into seperate
> strings?
>
This should handle periods or commas as the separator.

a = "24,4 + 55,2 + 55 - 44,0"
=> "24,4 + 55,2 + 55 - 44,0"
a.scan /(\d+,?.?\d*)(?=\s|$)/
=> [["24,4"], ["55,2"], ["55"], ["44,0"]]


Jeppe Jakobsen

2/11/2006 11:49:00 PM

0

Nice, that was the thing I was looking for :)

2006/2/12, Wilson Bilkovich <wilsonb@gmail.com>:
>
> On 2/4/06, Jeppe Jakobsen <jeppe88@gmail.com> wrote:
> > Hi all, how do you scan a string and avoid getting my decimal numbers
> > divided into 2 numbers.
> >
> > Example:
> >
> > a = "24,4 + 55,2"
> > a.scan! (/\d+/)
> > puts a
> >
> > my output for a will be:
> > 24
> > 4
> > 55
> > 2
> >
> > But I want to keep my decimal numbers intact like this:
> > 24,4
> > 55,2
> >
> >
> > How do I solve this problem without putting the numbers into seperate
> > strings?
> >
> This should handle periods or commas as the separator.
>
> a = "24,4 + 55,2 + 55 - 44,0"
> => "24,4 + 55,2 + 55 - 44,0"
> a.scan /(\d+,?.?\d*)(?=\s|$)/
> => [["24,4"], ["55,2"], ["55"], ["44,0"]]
>
>

Alexis Reigel

2/12/2006 12:41:00 AM

0

>
> This should handle periods or commas as the separator.
>
> a = "24,4 + 55,2 + 55 - 44,0"
> => "24,4 + 55,2 + 55 - 44,0"
> a.scan /(\d+,?.?\d*)(?=\s|$)/
> => [["24,4"], ["55,2"], ["55"], ["44,0"]]
>

Some problems here:
- signs are disregarded ("-24,4" becomes "24,4")
- Invalid numbers are accepted: eg. "24,.4" "24,." "24." "24,"
- "." should be escaped. As you used it here, it means "any character"
(except newline), so many invalid numbers are accepted (e.g. "24w"...)
- If something different from whitespace follows the number, it is not
or false accepted, e.g. "24.4." becomes "4." instead of "24.4"
- ...


Alexis.