code_junkie

Regular expression to test whether a string consists of a valid real number in base 10

Nov 11th, 2011
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. "1" yes
  2. "-1" yes
  3. "- 3" no
  4. "1.2" yes
  5. "1.2.3" no
  6. "7e4" no (though in some cases you may want to allow scientific notation)
  7. ".123" yes
  8. "123." yes
  9. "." no
  10. "-.5" yes
  11. "007" yes
  12. "00" yes
  13.  
  14. /^s*[+-]?(?:d+.?d*|d*.d+)s*$/
  15.  
  16. /^[-+]?(?=.?d)d*(?:.d*)?$/
  17.  
  18. /^[-+]?(?:.d+|d+(?:.d*)?)$/
  19. /^[-+]?(.d+|d+(.d*)?)$/ # if you don't mind capturing parens
  20.  
  21. /^[-+]?(?:.d+|(?:0|[1-9]d*)(?:.d*)?)$/
  22. /^[-+]?(.d+|(0|[1-9]d*)(.d*)?)$/
  23.  
  24. /^[-+]?(?:.[0-9]+|(?:0|[1-9][0-9]*)(?:.[0-9]*)?)$/
  25. /^[-+]?(.[0-9]+|(0|[1-9][0-9]*)(.[0-9]*)?)$/
  26.  
  27. ^[+-]?(?:d+(?:.d*)?|.d+)$
  28.  
  29. ^[+-]?(?:d+(?:.d+)?|.d+)$
  30.  
  31. d*
  32.  
  33. /^s*[-+]?(d+|d*.d+)s*$/
Add Comment
Please, Sign In to add comment