Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

regexp for finding floats in a string

37 views
Skip to first unread message

Bil Kleb

unread,
Aug 14, 2006, 6:37:39 PM8/14/06
to
What RegExp should I be using for floats?

Currently, I have /[-+0-9eE.]+/, viz,

require 'test/unit'
class TestFloatRegExp < Test::Unit::TestCase
def test_finds_em
%w{ 1 -2 3.0 0.4 5.0e+5 -6e+06 .7E+7 }.each do |string|
assert( string.match(/[-+0-9eE.]+/), "no match >>#{string}<<" )
end
end
end

--
Bil
http://fun3d.larc.nasa.gov

Xavier Noria

unread,
Aug 14, 2006, 6:56:21 PM8/14/06
to
On Aug 15, 2006, at 12:50 AM, Bil Kleb wrote:

> What RegExp should I be using for floats?
>
> Currently, I have /[-+0-9eE.]+/, viz,
>
> require 'test/unit'
> class TestFloatRegExp < Test::Unit::TestCase
> def test_finds_em
> %w{ 1 -2 3.0 0.4 5.0e+5 -6e+06 .7E+7 }.each do |string|
> assert( string.match(/[-+0-9eE.]+/), "no match >>#{string}<<" )
> end
> end
> end

Have a look at the regexp in

perldoc -q float

-- fxn

PS: Your mail client runs on a Mac, that works there.


Bil Kleb

unread,
Aug 16, 2006, 9:41:55 AM8/16/06
to
Xavier Noria wrote:
> Have a look at the regexp in
>
> perldoc -q float

/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/

Ouch!

Thanks; I think. :)

Regards,
--
Bil
http://fun3d.larc.nasa.gov

A LeDonne

unread,
Aug 17, 2006, 2:04:43 PM8/17/06
to
On 8/16/06, Bil Kleb <Bil....@nasa.gov> wrote:
> Xavier Noria wrote:
> > Have a look at the regexp in
> >
> > perldoc -q float
>
> /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/
>
> Ouch!
>
> Thanks; I think. :)

Quick caveat - that's for a c float (as mentioned in that perldoc
snippet). Ruby float (& other numeric) literals allow the underscore
character (with restrictions) as well. So really, it depends on
exactly what you're parsing and why.

-A

0 new messages