Sunday, January 4, 2009

Regex for matching floating point numbers

As simple as it sounds, there doesn't seem to be a standard regular expression for matching floating point numbers -- for some reason everyone seems to have their own preference: scientific notation or not, decimal points in the exponent or not... etc. Currently I'm settling with
([-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]*\.?[0-9]+)?)

Actually for my purposes, I also need an optional unit, so I'm really using this
([-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]*\.?[0-9]+)?)([a-zA-Z]*)

No comments: