Let's look at the actual regex that is used:
^([+\-]?[0-9\,]*(\.[0-9]*)?)$
This matches 12.
because your second part is (\.[0-9]*)
. Note that *
means zero or more, so digits are optional.
This also matches 1,,2
because you included the comma in the first character class [0-9\,]
. So actually your regex would match ,,,,,,,,
as well.
This can be solved without regexes, but if you need a regex, you'd probably want something like this:
^[+-]?[0-9]{1,3}(,[0-9]{3})*(\.[0-9]+)?$
Broken down:
^ # match start of string
[+-]? # matches optional + or - sign
[0-9]{1,3} # match one or more digits
(,[0-9]{3})* # match zero or more groups of comma plus three digits
(\. # match literal dot
[0-9]+ # match one or more digits
)? # makes the decimal portion optional
$ # match end of string
To use this in Java you'd want something like:
ets = ","; // commas don't need to be escaped
eds = "\\."; // matches literal dot
regex = "^[+-]?[0-9]{1,3}(" + ets + "[0-9]{3})*(" + eds + "[0-9]+)?$"