1
1000 - valid
1,000 - valid
1,000.00 - valid
1000.00 - valid
1000.00.00 - invalid
1,0.00 - invalid
1,000,00.00 - invalid
1,000,000.12 - valid

no of decimal places can be unlimited

I've been trying to find the right regex pattern, can't seem to find one that will accomodate all validations. Can anyone help

the pattern ^[1-9]\d{0,2}(.\d{3})*(,\d+)?$ did not work for me, based from the similar thread here

Community
  • 1
  • 1
  • 1
    Possible duplicate of [Decimal or numeric values in regular expression validation](http://stackoverflow.com/questions/2811031/decimal-or-numeric-values-in-regular-expression-validation) – royB Mar 15 '17 at 23:32
  • the pattern ^[1-9]\d{0,2}(.\d{3})*(,\d+)?$ did not work for me, based from that similar thread – Hingle McJingleberry Mar 15 '17 at 23:38
  • @Alain Del Rosario try using ^[1-9]\d*(\,\d+)?$ . You can try the regex script with sample input on this site http://www.regexplanet.com/advanced/java/index.html – Arthur Decker Mar 15 '17 at 23:41
  • @Arthur Decker did not work – Hingle McJingleberry Mar 15 '17 at 23:50
  • i think validations from that link treats commas as required not optional – Hingle McJingleberry Mar 15 '17 at 23:54
  • If there are thousand separators before the decimal point, do they need to be there after the point too? Are both 1,111,111.111,111,1 and 1,111,111.1111111 valid? – Ole V.V. Mar 16 '17 at 00:00
  • @AlainDelRosario, please remember to accept an answer if you find one of the four acceptable. Excuse me for mentioning, but it seems you were good at accepting answers in January, I hope you are not starting to forget? – Ole V.V. Mar 19 '17 at 19:01

4 Answers4

3

This is one of possible regexes you are looking for:

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

Demo: https://regex101.com/r/T8tcDP/1

streetturtle
  • 5,472
  • 2
  • 25
  • 43
3

You should try this expression:

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

With this expression is covered with the scenarios raised.

Here the complete example:

public class Decimal {

    private static String REGEX = "^\\d{1,3}|\\d(([ ,]?\\d{3})*([.,]\\d{2}+)?$)";

    public static void main(String[] args) {
        String data[] = {"1000", "1,000", "1,000.00", "1000.00", "1000.00.00", "1,0.00", "1,000,00.00", "1,000,000.12"};

        Pattern.compile(REGEX);

        for (int i = 0; i < data.length; i++) {
            if (data[i].matches(REGEX)) {
                System.out.println(data[i] + " - valid");
            } else {
                System.out.println(data[i] + " - invalid");
            }
        }
    }

}

The output:

  • 1000 - valid
  • 1,000 - valid
  • 1,000.00 - valid
  • 1000.00 - valid
  • 1000.00.00 - invalid
  • 1,0.00 - invalid
  • 1,000,00.00 - invalid
  • 1,000,000.12 - valid
JUAN CALVOPINA M
  • 3,695
  • 2
  • 21
  • 37
0

This would match your numbers there (?:\d+(?:,\d{3})*(?:\.\d{2})?|\.\d{2})
It would also match .00 just incase. If you don't want it to, just remove
the |\.\d{2} part.

Add your own boundary constructs as needed ^$ or \b

Expanded

 (?:
      \d+ 
      (?: , \d{3} )*
      (?: \. \d{2} )?
   |  \. \d{2} 
 )
0

My suggestion is:

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

A number either has separators for every 3 digits or it hasn’t (no middle forms). It either has a decimal point and at least one digit after it, or it hasn’t. In all cases does it start with a non-zero digit.

Remember that a dot (period, .) has a special meaning in regex and therefore needs to be escaped to get a literal point.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161