|
This is what I use, it also deals with leading/trailing whitespace.
function isnumber( str )
local n1,n2 = str:match( "^%s*[+-]?(%d*)[.]?(%d*)%s*$" )
if n1 == nil then
return false
end
-- %d* can match a 0 length string so make sure
-- that at least some digits were matched
if length(n1) == 0 and length(n2) == 0 then
return false
end
return true
endOn Tue, Dec 7, 2010 at 4:03 AM, Dirk Laurie <dpl@sun.ac.za> wrote:
I'm writing an input checker for a non-Lua application. It makes
liberal use of positive F-format real numbers, e.g. 3, .1, 3., 3.1.
Can one do that with just one pattern, assuming whitespace already
trimmed?
"%d*%.?%d*" matches any string
"^%d*%.?%d*$" matches '.' and ''
"^%d+%.?%d*$" does not match .1
"^%d*%.?%d+$" does not match 3.
Dirk