Take the 2-minute tour ×

I'm looking for a regular expression which whill validate the number starting from 0 up - and might include decimals.

Any idea?

share|improve this question
A bit of a vague question, IMO. A decimal number is, strictly speaking, a number expressed in the decimal number system, while I guess you actually meant a decimal fraction (with a . or ,). Could you clarify and give a couple of examples of valid and invalid strings? –  Bart Kiers Jan 31 '11 at 13:30

6 Answers 6

up vote 2 down vote accepted

A simple regex to validate a number:


This should work for a number with optional leading zeros, with an optional single dot and more digits.

  • ^...$ - match from start to end of the string (will not validate ab12.4c)
  • \d+ - At least one digit.
  • (...)? - optional group of...
  • \.\d+ - literal dot and one or more digits.
share|improve this answer
Hi Kobi - this works like a charm - many thanks! –  user398341 Jan 31 '11 at 13:29
That doesn’t work on the decimal numbers accepted by compilers like C, Perl, or Java. You need to make it accept all of these: 42, 42., 4.2, and .42. –  tchrist Jan 31 '11 at 14:58
@tchrist - Hmm. I appreciate the enthusiasm, but isn't that a matter of decision? On every case you can decide if you need all of these literals - if they are valid for the OP in this case or not. I don't need to support C and Perl, do I? .42 is nice to have and missing from my regex (\d*\.?\d+ can solve it though, or something more efficient). By the way, in C# 42. isn't valid - you can have a look at the grammar under C.1.8 Literals > real-literal. Also, 42. is weird :) –  Kobi Jan 31 '11 at 19:49
It’s not just C and Perl. It’s also awk, go, C++, Python, Java, and probably a great many other programming languages. It’s follows from the philosophy of being liberal in what you accept. As for 42., that has been the standard way since time immemorial — or FORTRAN, whichever comes first — of distinguishing a float from an int. As far the rest, well, I’m like a good housekeeper in that I don’t do [ᴍ$ꜰᴛ] ᴡɪɴᴅᴏᴡ﹩: never had it, never will. It’s dead to me. –  tchrist Jan 31 '11 at 21:21

Because decimal numbers may or may not have a decimal point in them, and may or may not have digits before that decimal point if they have some afterwards, and may or may not have digits following that decimal point if they have some before it, you must use this:


which is usually better written:


and much better written:

^                       # anchor to start of string
(?:                     # EITHER
    \d+ (?: \. \d* )?   #   some digits, then optionally a decimal point following by optional digits
  |                     # OR ELSE
    \d*     \. \d+      #   optional digits followed then a decimal point and more digits
)                       # END ALTERNATIVES
$                       # anchor to end of string

If your regex compiler doesn’t support \d, or also depending on how Unicode-aware your regex engine is if you should prefer to match only ASCII digits instead of anything with the Unicode Decimal_Number property (shortcut Nd) — that is, anything with the Numeric_Type=Decimal property — then you might wish to swap in [0-9] for all instances above where I’ve used \d.

share|improve this answer

I always use RegExr to build my regular expressions. It is sort of drag-and-drop and has a live-preview of your regex and the result.

It'll look something like ^0[,.0-9]*

share|improve this answer
This one really doesn't do - as it validates the following : '0.5a', which contains the letter –  user398341 Jan 31 '11 at 13:26
Well, yeah, that why I said something like, and not "This is the solution:" ;-) –  Zsub Jan 31 '11 at 13:27
That validates 0,42.,...,,.12.411...,,.42,,1,,,1,,42McGillicuddy, which surely is no help at all. –  tchrist Jan 31 '11 at 15:31

Note that with this expression 0.1 will be valid but .1 won't.

share|improve this answer

This should do what you want:


It will match any number starting with 0 and then any number, maybe a , or . and any number

share|improve this answer
This would allow etc –  El Ronnoco Jan 31 '11 at 13:25
That matches 0........,,,,,,,,......... too, to name just one invalid number. –  Bart Kiers Jan 31 '11 at 13:25
This one doesn't seem to like 0 –  user398341 Jan 31 '11 at 13:26
I updated the regex. –  morja Jan 31 '11 at 13:26
So 01000 would be valid but 1000 wouldn't... –  El Ronnoco Jan 31 '11 at 13:31

will match if the test string is a positive decimal number

share|improve this answer
It matches more than that: ................. for example. –  Bart Kiers Jan 31 '11 at 13:26

Your Answer


