Java regular expression for checking comma-separated numeric values

I need to make a regex with real numbers like this:

 "+1", "1.0", "1,233", "1,233,456.34", "-1", ".34", "1,345,234,122,123"

and these invalid values ​​are:

 "++1", "1.0.0", "1,23,3", "+-1233456.34", "002", "1.", "a1", "1,,2", "1 2", "1,2", ",2".

I tried different variations of this regex:

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

Code for testing:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class REGEX {
private static final String REGEX = "[\\+\\-]?[1-9]{0,3}([\\d]{3}[\\,]{1})?([\\d]{3}[\\,]{1})?([\\d]{3}[\\,]{1})?([\\d]{3}[\\,]{1})?([\\d]{3}[\\,]{1})?([\\.][\\d]*)?";

private static String[] validNumbers = { "+1", "1.0", "1,233",
        "1,233,456.34", "-1", ".34", "1,345,234,122,123" };

private static String[] invalidNumbers = { "++1", "1.0.0", "1,23,3",
        "+-1233456.34", "002", "1.", "a1", "1,,2", "1 2", "1,2", ",2" };

public static void main(String[] args) {
    Pattern pattern = Pattern.compile(REGEX);

    for (String number : validNumbers) {
        Matcher matcher = pattern.matcher(number);
        if (!matcher.matches()) {
            System.out.println("Valid number is detected as invalid: "
                    + number);
        }
    }
    for (String number : invalidNumbers) {
        Matcher matcher = pattern.matcher(number);
        if (matcher.matches()) {
            System.out.println("Invalid number is detected as valid: "
                    + number);
        }
    }
}

}

When the console is empty, the task will be completed. Now I have such problems:

Valid number detected as invalid: 1,233

Valid number detected as invalid: 1,233,456.34

Valid number detected as invalid: 1,345,234,122,123

Invalid number is defined as valid: 1.

Sincerely. Sorry for the large size.

Update . Thanks to Noob UnChained , I moved on to this regex:

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

and now there are fewer problems:

Valid number detected as invalid: 1,233,456.34

Valid number detected as invalid: .34

.

:

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

:

: 1.

FINISHED

:

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

: ([.] [\ d] +)?

plus , , , .

: 2 "\" , .

: user2266098 nhahtdh.

user2266098 "0,1" "| 0" . "+" "-" (- "()" "[]" ). "{0,}" "*" - .

nhahtdh "(?! $)".

!

:

:

"+1", "1.0", "1,233", "1,233,456.34", "-1", ".34", "1,345,234,122,123", "0.1"

:

"++1", "1.0.0", "1,23,3", "+-1233456.34", "002", "1.", "a1", "1,,2", "1 2", "1,2", ",2", "", "0,123"

"" regexp =)

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

: : 0,123

+5
2

FINISHED

:

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

: ([.] [\ d] +)?

plus , , , .

: 2 "\" , .

: user2266098 nhahtdh.

user2266098 "0,1" "| 0" . "+" "-" . "{0,}" "*" - .

nhahtdh "(?! $)".

!

:

:

"+1", "1.0", "1,233", "1,233,456.34", "-1", ".34", "1,345,234,122,123", "0.1"

:

"++1", "1.0.0", "1,23,3", "+-1233456.34", "002", "1.", "a1", "1,,2", "1 2", "1,2", ",2", "", "0,123"

"" regexp =)

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

: : 0,123

+4

:

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

btw , 0,1, , . ,

+3

All Articles