Dzyann
Dzyann

Reputation: 5208

Regular expression to match number with Decimal separator and optional Thousands separator

I need to validate if a number is valid with using decimal separator and thousand separators optionally. I need a regex, because the current code, that is not done by me uses a regex that is not working properly and I am aiming to fix it.

So the following combinations would be legal (Using . as decimal separator and , as thousand separator)

But the following wouldn't be legal:

Basically I want numbers with a proper use of decimal and thousand separator or without them at all, but not invalid combinations. I looked around a lot, so far I found this post. Which has a great regex that matches perfectly when the numbers have decimal separator and thousand separator, but when I try to add the other options (numbers only with decimal separator, or number integers) I break the expression.

I decided that maybe I could capture groups matching the rules with or, so the number matches one expression or the other like this:

^([+-]?[0-9]{1,3}(,[0-9]{3})*(\.[0-9]+)?)|(((\d)*)+?(\.\d))|(\d*)$

The first group is the expression from the other post, the second I think covers numbers with a decimal separator and the last one are integers, but it is not working.

For example these numbers are not getting matched:

I think the issue it is happening because it always test for the first group, but I am not sure how to use the groups properly so it matches either of them. Also I think I am placing the -+ signs in the wrong place.

I did search a lot, and couldn't find one expression that worked properly. From the ones suggested:

From the Regex for number with decimals and thousand separator post

This Can I use an OR in regex without capturing what's enclosed? post is not a duplicate, it explains something that could help me solve the issue, but it is not a duplicate, especially because I have issues also with the +- signs. Also my questions covers a problem that is not particular to me, this is a problem many people face, yet from the implementations I have found so far, none work.

From RegEx matching numeric values with or without thousand separators post

Upvotes: 12

Views: 9581

Answers (2)

anubhava
anubhava

Reputation: 785196

You may use this regex for validation:

^[+-]?(?:\d+|\d{1,3}(?:,\d{3})*)(?:\.\d*)?$

RegEx Demo

It matches following cases:

  • An integer number
  • A floating point number
  • A number with separators as , at 3rd place

Upvotes: 8

Barmar
Barmar

Reputation: 781068

The reason the second alternative isn't matching is because it only allows a single \f after the decimal point. That needs to be \d+.

Then you need to wrap everything between ^ and $ in a group, so all alternatives match the entire string.

You had lots of redundant parentheses. And \d* in the last alternative should be \d+, otherwise you'll allow a number that's completely empty or just a sign.

^[+-]?([0-9]{1,3}(,[0-9]{3})*(\.[0-9]+)?|\d*\.\d+|\d+)$
  • ^ -> start of string
  • [+-]? -> matches optional + or - char
  • ([0-9]{1,3}(,[0-9]{3})*(\.[0-9]+)?|\d*\.\d+|\d+) -> whole group has to match [0-9]{1,3}(,[0-9]{3})*(\.[0-9]+) or \d*\.\d+ or \d+
    • [0-9]{1,3}(,[0-9]{3})*(\.[0-9]+) -> matches numbers with thousand separators and maybe decimal separator
    • \d*\.\d+ -> matches numbers with decimal separator, and maybe digits before the decimal
    • \d+ -> matches numbers without decimal separator
  • $ -> end of string

DEMO

Upvotes: 15

Related Questions