Q:

Java regex for String containing decimals seperated by comma

I am making Java app where user enters a decimal seperated by comma like: -343.123,0.32 or 3,-321 used for Latitude and Longitude like:
Latitude,Longitude where both Latitude and Longitude are double
Valid string: 43,-23.123 or 32.312,32.33
I have tried this regex, but it is not working:
str.matches("^[-0-9.0-9][,][-0-9.0-9]$")

A:

A sloppy match (allows multiple - and . characters):
"^[-+0-9.]+,[-+0-9.]+$"

A more exact one:
"^[-+]?[0-9]+\.?[0-9]*,[-+]?[0-9]+\.?[0-9]*$"

Special characters
? : zero or more matches of preceding
+ : one or more matches of preceding (except when in character set)
* : zero or more matches of preceding

Hope that helps. Change the match character on the integer portion of the numbers to * if you expect lat/long without integer (very unlikely).

