Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Scanning a string for decimal numbers

Reply
Thread Tools

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--


 
Reply With Quote
 
 
 
 
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?
>
>



 
Reply With Quote
 
 
 
 
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)
 
Reply With Quote
 
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

--
happiness is not something ready-made. it comes from your own actions.
- h.h. the 14th dali lama


 
Reply With Quote
 
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.

 
Reply With Quote
 
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--


 
Reply With Quote
 
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"]]


 
Reply With Quote
 
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--


 
Reply With Quote
 
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.



 
Reply With Quote
 
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--


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
decimal.Decimal formatting python@lists.fastmail.net Python 0 07-19-2010 12:32 PM
how to convert from Decimal('1.23456789') to Decimal('1.234') valpa Python 11 03-24-2009 07:11 AM
Error: Cannot convert Decimal("0.0000") to Decimal Vitaliy Python 1 05-29-2008 10:36 AM
TypeError: unsupported operand type(s) for -: 'Decimal' and 'Decimal'. Why? Gilbert Fine Python 8 08-01-2007 01:58 AM
Decimal to Packed Decimal Conversion in C++ Ven C++ 3 08-01-2006 03:56 PM



Advertisments