Question

i've been trying to validate the correct format for coordinate. Currently i want to save the coordinate. But when the format is wrong, the markers wont appear on my map.

This is the some of my sample code.

String latlon = "3.169054, 101.714108";
        String regex_coords = "/\\((\\-?\\d+\\.\\d+), (\\-?\\d+\\.\\d+)\\)/";
        Pattern compiledPattern2 = Pattern.compile(regex_coords, Pattern.CASE_INSENSITIVE);
        Matcher matcher2 = compiledPattern2.matcher(latlon);
        while (matcher2.find()) {
            System.out.println("Is Valid Map Coordinate: " + matcher2.group());
        }

I want the user to be able to save this format only "3.169054, 101.714108" with the comma.

Can anyone help me build the regex to valid this format?

Thanks all

Was it helpful?

Solution

This should work for you:

([+-]?\d+\.?\d+)\s*,\s*([+-]?\d+\.?\d+)

Online RegexPlanet Demo: http://fiddle.re/20pm

This permits any coordinate to be a number which optionally starts with + or -. Number can be either integer or double (but dot must be enclosed with digits if present).

Finally, you can get latitude and longitude as group1 and group2.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top