Skip to content

Instantly share code, notes, and snippets.

@ferrous26
Last active January 3, 2016 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ferrous26/8485477 to your computer and use it in GitHub Desktop.
Save ferrous26/8485477 to your computer and use it in GitHub Desktop.
A regular expression for validating floating point numbers
DIGITS = '(\d+)'
EXPONENT = "([eE][+-]?#{DIGITS})"
SUFFIX = '(f|F|d|D)'
PATTERN = Regexp.union [
"#{DIGITS}\\.#{DIGITS}?#{EXPONENT}?#{SUFFIX}?",
"\\.#{DIGITS}#{EXPONENT}?#{SUFFIX}?",
"#{DIGITS}#{EXPONENT}#{SUFFIX}?",
"#{DIGITS}#{EXPONENT}?#{SUFFIX}"
].map { |str| Regexp.new "\\A#{str}\\Z" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment