136
votes

I need a regular expression that validates a number, but doesn't require a digit after the decimal. ie.

123
123.
123.4

would all be valid

123..

would be invalid

Any would be greatly appreciated!

15
The best answer is here: stackoverflow.com/a/39399503/715269Gangnus

15 Answers

212
votes

Use the following:

/^\d*\.?\d*$/
  • ^ - Beginning of the line;
  • \d* - 0 or more digits;
  • \.? - An optional dot (escaped, because in regex, . is a special character);
  • \d* - 0 or more digits (the decimal part);
  • $ - End of the line.

This allows for .5 decimal rather than requiring the leading zero, such as 0.5

135
votes
/\d+\.?\d*/

One or more digits (\d+), optional period (\.?), zero or more digits (\d*).

Depending on your usage or regex engine you may need to add start/end line anchors:

/^\d+\.?\d*$/

Regular expression visualization

Debuggex Demo

87
votes

You need a regular expression like the following to do it properly:

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

The same expression with whitespace, using the extended modifier (as supported by Perl):

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

or with comments:

/^           # Beginning of string
 [+-]?       # Optional plus or minus character
 (           # Followed by either:
   (           #   Start of first option
     \d+       #   One or more digits
     (\.\d*)?  #   Optionally followed by: one decimal point and zero or more digits
   )           #   End of first option
   |           # or
   (\.\d+)     #   One decimal point followed by one or more digits
 )           # End of grouping of the OR options
 $           # End of string (i.e. no extra characters remaining)
 /x          # Extended modifier (allows whitespace & comments in regular expression)

For example, it will match:

  • 123
  • 23.45
  • 34.
  • .45
  • -123
  • -273.15
  • -42.
  • -.45
  • +516
  • +9.8
  • +2.
  • +.5

And will reject these non-numbers:

  • . (single decimal point)
  • -. (negative decimal point)
  • +. (plus decimal point)
  • (empty string)

The simpler solutions can incorrectly reject valid numbers or match these non-numbers.

17
votes

Try this regex:

\d+\.?\d*

\d+ digits before optional decimal
.? optional decimal(optional due to the ? quantifier)
\d* optional digits after decimal

14
votes

this matches all requirements:

^\d+(\.\d+)?$
5
votes

I ended up using the following:

^\d*\.?\d+$

This makes the following invalid:

.
3.
4
votes

you can use this:

^\d+(\.\d)?\d*$

matches:
11
11.1
0.2

does not match:
.2
2.
2.6.9

3
votes

This is what I did. It's more strict than any of the above (and more correct than some):

^0$|^[1-9]\d*$|^\.\d+$|^0\.\d*$|^[1-9]\d*\.\d*$

Strings that passes:

0
0.
1
123
123.
123.4
.0
.0123
.123
0.123
1.234
12.34

Strings that fails:

.
00000
01
.0.
..
00.123
02.134
3
votes
^[+-]?(([1-9][0-9]*)?[0-9](\.[0-9]*)?|\.[0-9]+)$

should reflect what people usually think of as a well formed decimal number.

The digits before the decimal point can be either a single digit, in which case it can be from 0 to 9, or more than one digits, in which case it cannot start with a 0.

If there are any digits present before the decimal sign, then the decimal and the digits following it are optional. Otherwise, a decimal has to be present followed by at least one digit. Note that multiple trailing 0's are allowed after the decimal point.

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

correctly matches the following:

9
0
10
10.
0.
0.0
0.100
0.10
0.01
10.0
10.10
.0
.1
.00
.100
.001

as well as their signed equivalents, whereas it rejects the following:

.
00
01
00.0
01.3

and their signed equivalents, as well as the empty string.

2
votes

What language? In Perl style: ^\d+(\.\d*)?$

1
votes
(?<![^d])\d+(?:\.\d+)?(?![^d])

clean and simple.

This uses Suffix and Prefix, RegEx features.

It directly returns true - false for IsMatch condition

1
votes
^\d+(()|(\.\d+)?)$

Came up with this. Allows both integer and decimal, but forces a complete decimal (leading and trailing numbers) if you decide to enter a decimal.

1
votes

What you asked is already answered so this is just an additional info for those who want only 2 decimal digits if optional decimal point is entered:

^\d+(\.\d{2})?$

^ : start of the string
\d : a digit (equal to [0-9])
+ : one and unlimited times

Capturing Group (.\d{2})?
? : zero and one times . : character .
\d : a digit (equal to [0-9])
{2} : exactly 2 times
$ : end of the string

1 : match
123 : match
123.00 : match
123. : no match
123.. : no match
123.0 : no match
123.000 : no match
123.00.00 : no match

1
votes

try this. ^[0-9]\d{0,9}(\.\d{1,3})?%?$ it is tested and worked for me.

0
votes

In Perl, use Regexp::Common which will allow you to assemble a finely-tuned regular expression for your particular number format. If you are not using Perl, the generated regular expression can still typically be used by other languages.

Printing the result of generating the example regular expressions in Regexp::Common::Number:

$ perl -MRegexp::Common=number -E 'say $RE{num}{int}'
(?:(?:[-+]?)(?:[0123456789]+))

$ perl -MRegexp::Common=number -E 'say $RE{num}{real}'
(?:(?i)(?:[-+]?)(?:(?=[.]?[0123456789])(?:[0123456789]*)(?:(?:[.])(?:[0123456789]{0,}))?)(?:(?:[E])(?:(?:[-+]?)(?:[0123456789]+))|))

$ perl -MRegexp::Common=number -E 'say $RE{num}{real}{-base=>16}'
(?:(?i)(?:[-+]?)(?:(?=[.]?[0123456789ABCDEF])(?:[0123456789ABCDEF]*)(?:(?:[.])(?:[0123456789ABCDEF]{0,}))?)(?:(?:[G])(?:(?:[-+]?)(?:[0123456789ABCDEF]+))|))