regexp for finding floats in a string

B

Bil Kleb

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
 
X

Xavier Noria

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

A LeDonne

Xavier said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,206
Latest member
SybilSchil

Latest Threads

Top