Velocity Reviews > Ruby > Scanning a string for decimal numbers

# Scanning a string for decimal numbers

Jeppe Jakobsen
Guest
Posts: n/a

 02-04-2006
------=_Part_20441_2617855.1139091631058
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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

Example:

a =3D "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?

------=_Part_20441_2617855.1139091631058--

Kent Sibilev
Guest
Posts: n/a

 02-04-2006
"24,4 + 55,2".scan /[\d,]+/

Kent

On 2/4/06, Jeppe Jakobsen <(E-Mail Removed)> wrote:
> Hi all, how do you scan a string and avoid getting my decimal numbers
> divided into 2 numbers.
>
> Example:
>
> a =3D "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
Guest
Posts: n/a

 02-04-2006
Kent Sibilev wrote:
> "24,4 + 55,2".scan /[\d,]+/
>
> Kent
>
> On 2/4/06, Jeppe Jakobsen <(E-Mail Removed)> 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@noaa.gov
Guest
Posts: n/a

 02-05-2006
On Sun, 5 Feb 2006, Ernest Ellingson wrote:

> Kent Sibilev wrote:
>> "24,4 + 55,2".scan /[\d,]+/
>>
>> Kent
>>
>> On 2/4/06, Jeppe Jakobsen <(E-Mail Removed)> 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

careful. you'll kill negatives.

-a

--
- h.h. the 14th dali lama

Josef 'Jupp' SCHUGT
Guest
Posts: n/a

 02-11-2006
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
Guest
Posts: n/a

 02-11-2006
------=_Part_1510_14941765.1139699553034
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Will that expression include both integers and decimal numbers?

------=_Part_1510_14941765.1139699553034--

Wilson Bilkovich
Guest
Posts: n/a

 02-11-2006
On 2/4/06, Jeppe Jakobsen <(E-Mail Removed)> wrote:
> Hi all, how do you scan a string and avoid getting my decimal numbers
> divided into 2 numbers.
>
> Example:
>
> a =3D "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 =3D "24,4 + 55,2 + 55 - 44,0"
=3D> "24,4 + 55,2 + 55 - 44,0"
a.scan /(\d+,?.?\d*)(?=3D\s|\$)/
=3D> [["24,4"], ["55,2"], ["55"], ["44,0"]]

Jeppe Jakobsen
Guest
Posts: n/a

 02-11-2006
------=_Part_1666_16346009.1139701742380
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Nice, that was the thing I was looking for

2006/2/12, Wilson Bilkovich <(E-Mail Removed)>:
>
> On 2/4/06, Jeppe Jakobsen <(E-Mail Removed)> wrote:
> > Hi all, how do you scan a string and avoid getting my decimal numbers
> > divided into 2 numbers.
> >
> > Example:
> >
> > a =3D "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 =3D "24,4 + 55,2 + 55 - 44,0"
> =3D> "24,4 + 55,2 + 55 - 44,0"
> a.scan /(\d+,?.?\d*)(?=3D\s|\$)/
> =3D> [["24,4"], ["55,2"], ["55"], ["44,0"]]
>
>

------=_Part_1666_16346009.1139701742380--

Alexis Reigel
Guest
Posts: n/a

 02-12-2006
>
> 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.

Jeppe Jakobsen
Guest
Posts: n/a

 02-12-2006
------=_Part_2018_3018110.1139707906280
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

2006/2/12, Alexis Reigel <(E-Mail Removed)>:
>
> >
> > This should handle periods or commas as the separator.
> >
> > a =3D "24,4 + 55,2 + 55 - 44,0"
> > =3D> "24,4 + 55,2 + 55 - 44,0"
> > a.scan /(\d+,?.?\d*)(?=3D\s|\$)/
> > =3D> [["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.
>
>
>

Let me see if I got it right then. I'll like to use periods only for my
decimal numbers. I also need normal integers so 24. being accepted won't
matter. Will this fix the problems you presented?:
/[-+]?(\d+\.?\d*)(?=3D\s|\$)/

I don't know if it takes care of the last problem, because I didn't
understand it.

------=_Part_2018_3018110.1139707906280--