regular expression for finding decimal/float numbers?

79,748

Solution 1

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:

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

RegexPal

Solution 2

The right expression should be as followed:

[+-]?([0-9]*[.])?[0-9]+

this apply for:

+1
+1.
+.1
+0.1
1
1.
.1
0.1

Here is Python example:

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

Output:

True
1.0

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

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

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

Solution 3

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
}

Solution 4

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:

/^((-)?(0|([1-9][0-9]*))(\.[0-9]+)?)$/

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

/^((\+|-)?(0|([1-9][0-9]*))(\.[0-9]+)?)$/

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:

/((\b|\+|-)(0|([1-9][0-9]*))(\.[0-9]+)?)\b/

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 127.0.0.1.

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 127.0.0.1. 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.

Solution 5

Paulpro and lbsweek answers led me to this:

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

re.exec("1")
>> Array [ "1" ]

re.exec("1.5")
>> Array [ "1.5" ]

re.exec("-1")
>> Array [ "-1" ]

re.exec("-1.5")
>> Array [ "-1.5" ]

re.exec(".5")
>> Array [ ".5" ]

re.exec("")
>> null

re.exec("qsdq")
>> null
Share:
79,748
MBehtemam
Author by

MBehtemam

Mohammad Bagher Ehtemam (MBehtemam) => MB Coding Loving MEMEX

Updated on November 13, 2021

Comments

  • MBehtemam
    MBehtemam over 2 years

    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.