regular expression for finding decimal/float numbers?

i need a regular expression for decimal/float numbers like 12 12.2 1236.32 123.333 and +12.00 or -12.00 or ...123.123... for using in javascript and jQuery. Thank you.



Optionally match a + or - at the beginning, followed by one or more decimal digits, optional followed by a decimal point and one or more decimal digits util the end of the string:




The right expression should be as followed:


this apply for:


Here is Python example:

import re
#print if found
print(bool('[+-]?([0-9]*[.])?[0-9]+', '1.0')))
#print result
print('[+-]?([0-9]*[.])?[0-9]+', '1.0').group(0))



If you are using mac, you can test on command line:

python -c "import re; print(bool('[+-]?([0-9]*[.])?[0-9]+', '1.0')))"

python -c "import re; print('[+-]?([0-9]*[.])?[0-9]+', '1.0').group(0))"

You can check for text validation and also only one decimal point validation using isNaN

var val = $('#textbox').val();
var floatValues =  /[+-]?([0-9]*[.])?[0-9]+/;
 if (val.match(floatValues) && !isNaN(val)) {
  // your function

This is an old post but it was the top search result for "regular expression for floating point" or something like that and doesn't quite answer _my_ question. Since I worked it out I will share my result so the next person who comes across this thread doesn't have to work it out for themselves.

All of the answers thus far accept a leading 0 on numbers with two (or more) digits on the left of the decimal point (e.g. 0123 instead of just 123) This isn't really valid and in some contexts is used to indicate the number is in octal (base-8) rather than the regular decimal (base-10) format.

Also these expressions accept a decimal with no leading zero (.14 instead of 0.14) or without a trailing fractional part (3. instead of 3.0). That is valid in some programing contexts (including JavaScript) but I want to disallow them (because for my purposes those are more likely to be an error than intentional).

Ignoring "scientific notation" like 1.234E7, here is an expression that meets my criteria:


or if you really want to accept a leading +, then:


I believe that regular expression will perform a strict test for the typical integer or decimal-style floating point number.

When matched:

  • $1 contains the full number that matched
  • $2 contains the (possibly empty) leading sign (+/-)
  • $3 contains the value to the left of the decimal point
  • $5 contains the value to the right of the decimal point, including the leading .

By "strict" I mean that the number must be the only thing in the string you are testing.

If you want to extract just the float value out of a string that contains other content use this expression:


Which will find -3.14 in "negative pi is approximately -3.14." or in "(-3.14)" etc.

The numbered groups have the same meaning as above (except that $2 is now an empty string ("") when there is no leading sign, rather than null).

But be aware that it will also try to extract whatever numbers it can find. E.g., it will extract 127.0 from

If you want something more sophisticated than that then I think you might want to look at lexical analysis instead of regular expressions. I'm guessing one could create a look-ahead-based expression that would recognize that "Pi is 3.14." contains a floating point number but Home is does not, but it would be complex at best. If your pattern depends on the characters that come after it in non-trivial ways you're starting to venture outside of regular expressions' sweet-spot.


Paulpro and lbsweek answers led me to this:

>> /^[+-]?(?:\d*\.)?\d+$/

>> Array [ "1" ]

>> Array [ "1.5" ]

>> Array [ "-1" ]

>> Array [ "-1.5" ]

>> Array [ ".5" ]

>> null

>> null


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.