Regex for Decimal Numbers

Decimal numbers come in all shapes and sizes, and a regular expression that matches all possible permutations might be more complicated than you think.

A regular expression for a decimal number needs to checks for one or more numeric characters (0-9) at the start of the string, followed by an optional period, and then followed by zero or more numeric characters (0-9). This should all be followed by an optional plus or minus sign.

The code for an expression that does all is:

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

So let’s discuss this step by step.

General Format of a Decimal Number

To accurately describe what a decimal number should look like, we need to use a split definition. As a start, we can say that a decimal number must obey the following guidelines:

  • Start with an optional sign (+ or – or nothing)
  • Followed by zero or more numbers (0-9)
  • Followed by an optional decimal point
  • Followed by zero or more numbers (0-9)

This will match decimal numbers like “9.432” and “2343.7” and integers like “2” and “56” but also something like “34.” and “.65” which most people and computers are able to recognize as a valid decimal value.

However, the guidelines above will also match a single period (.) or a single sign (+ or -) all by themselves. Therefore, as pointed out here, we need to split the definition into two:

  • Start with an optional sign (+ or – or nothing)
  • Followed by one or more numbers (0-9)
  • Followed by an optional decimal point
  • Followed by zero or more numbers (0-9)

Or

  • Start with an optional sign (+ or – or nothing)
  • Followed by a decimal point (not optional)
  • Followed by one or more numbers (0-9)

By following these guidelines, we can match any valid decimal number and exclude all unwanted characters.

Now let’s start converting these guidelines into a valid regular expression.

ALSO READ: Regex for Alphanumeric Characters

Regular Expressions for Decimal Numbers

Let’s work on the first part of the definition. At the start of the expression, we can have an optional sign (either + or -):

/[+-]/Edit with Regexity

We place the + and – sign in square brackets to show that we can match either one of them. However, we need to add the zero-or-one quantifier (?) to show that we can have either zero or one of these signs:

/[+-]?/Edit with Regexity

The optional sign can be followed by any numeric characters (including 0), so we add a character range (0-9) in square brackets to show that we can math any character in that range:

/[+-]?[0-9]/Edit with Regexity

We can quantify how much of these characters we want by adding a one-or-more quantifier (+) sign behind the square brackets.

/[+-]?[0-9]+/Edit with Regexity

This can be followed by an optional period (.) or decimal sign. A period is a reserved character in regular expression syntax and thus we should escape it with a backward slash preceding the period:

/[+-]?[0-9]+\./Edit with Regexity

This period character is optional, so we need to quantify it with a zero-or-one quantifier (?) behind it:

/[+-]?[0-9]+\.?/Edit with Regexity

And finally the decimal sign can be followed by zero or more number characters from 0 – 9. We again add these in square brackets to indicate that we’ll accept any character in this range, and we also add the zero-or-more quantifier (*) behind it to show that we’ll accept zero or more of them:

/[+-]?[0-9]+\.?[0-9]*/Edit with Regexity

So far so good for the first part of the definition. This will now match numbers like “1”, “1.2”, and “1.” as well as their signed counterparts (with a + or – in front).

Now for the second part (we’ll combine them at the end).

In the same way as above, we can start with an optional (zero or one) sign:

/[+-]?/Edit with Regexity

This can be followed by an optional period (.) or decimal point. Once again, we need to escape the period character with a backward slash in front of it. We don’t add a quantifier in this case, which shows that the period is definitely required in this definition:

/[+-]?\./Edit with Regexity

And finally, this can be followed by a range of numeric characters (0-9). We need at least one of these characters, so we can add the one-or-more quantifier (+) behind it:

/[+-]?\.[0-9]+/Edit with Regexity

This then completes the second part of the definition, and will match “.2” and any of it’s signed counterparts.

And now we must combine the first and second part of the definition into one. We can immediately spot that the optional sign is a common start to both definitions, meaning it can be matched in both expressions. Thus we can start out combined expression like this:

/[+-]?/Edit with Regexity

And then we can combine the remaining parts of the two definitions with an OR (|) characters (commonly called the pipe character) between them, and enclose the whole thing in round brackets to indicate a group:

/[+-]?([0-9]+\.?[0-9]*|\.[0-9]+)/Edit with Regexity

And finally, to ensure that this regex matches only a decimal number with nothing in front and nothing behind it, we can add the start-of-string (^) and end-of-string ($) characters to the start and end, respectively:

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

Other Considerations

The regex given above will match a decimal with nothing in front of it and nothing behind it, which is useful for validating a given expression as a decimal number. On the other hand, if you’d like to find all the decimal numbers on a page, you can remove the start-of-string (^) and end-of-string ($) characters, and add the global flag (g) at the back:

/[+-]?([0-9]+\.?[0-9]*|\.[0-9]+)/gEdit with Regexity

Another valid variation of this expression can be found by replacing the [0-9] numeric characters range with a digit characters symbol (\d):

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

This expression is somewhat shorter but provides less control over the range of numeric characters you want to allow. For example, if you don’t want any 0 before the decimal sign, you can alter the expression to allow only characters from the range 1 to 9:

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

Which Flags to Use

Since there are no alphabetic characters, you don’t need any flags for case sensitivity.

As described above, you might want to use the global flag (g) if you need to find all decimal numbers on a page.

Sources

The regular expressions on this page were adapted from solutions posted on Stack Overflow by Hoylen and Aaron Zorel on this question.

Benjamin

Founder, owner, and sole content creator on RegexLand. Enjoys programming, blogging, and teaching others how to do the same. Read more...

7 thoughts on “Regex for Decimal Numbers”

    • Hi Aditya. You could theoretically do it with something like /[0123]{1},[012]{1}/, which would match any decimal value with exactly one digit before the comma and one digit after. However, this is limited as it will not cover negative numbers, won’t match a valid number with two digits behind the comma (e.g. 3,2000 or 3,1999). It’s pretty limited. These kinds of tests are better done using comparison logic in your native language, such as if (number < 3.2 { //do something }

      Reply
  1. I’m somewhat puzzled by your solution for not wanting any 0 before the decimal sign. If you really meant no 0’s at all, your solution is right. But if your intention was to only disallow leading zero’s, I’m afraid the pattern needs some work. If I’m not mistaking, this would be the right pattern for those situations:

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

    Reply
  2. This solution has a bug.

    “`
    /^[+-]?([0-9]+\.?[0-9]*|\.[0-9]+)$/
    “`
    Should be:

    “`
    /^[+-]?([0-9]+(\.[0-9]+)?|\.[0-9]+)$/
    “`

    Numbers may start with a decimal point, but should not end with a decimal point.

    Reply

Leave a Comment