fork(1) download
  1. /*
  2. Parsear Geo Uri en Java
  3. https://es.stackoverflow.com/q/117661/127
  4. */
  5.  
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8.  
  9.  
  10. class Ideone
  11. {
  12. private static void parsearGeoURI(String s)
  13. {
  14. final String r = "^geo:([-+]?\\d+(?:\\.\\d+)?),([-+]?\\d+(?:\\.\\d+)?)(?:\\?z=(\\d{1,2}))?$";
  15. final Pattern p = Pattern.compile(r);
  16. final Matcher m = p.matcher(s);
  17.  
  18. if (m.find()) // Coincide con el regex
  19. {
  20. String lat = m.group(1),
  21. lon = m.group(2),
  22. zoom = m.group(3);
  23.  
  24. if (zoom == null) // Si no tiene zoom, no captura el 3er grupo del regex
  25. {
  26. zoom = "no tiene";
  27. }
  28.  
  29. System.out.format(
  30. "URI: %s%nLatitud: %s%nLongitud: %s%nZoom: %s%n%n",
  31. s, lat, lon, zoom
  32. );
  33. }
  34. else
  35. {
  36. System.out.format("URI: %s%n no es una geo URI%n%n", s);
  37. }
  38. }
  39.  
  40.  
  41. public static void main (String[] args) throws java.lang.Exception
  42. {
  43. // Pruebas
  44. final String[] pruebas = {
  45. "geo:79.786971,-124.399677",
  46. "geo:79.786971,-124.399677?z=99",
  47. "geo:79.786971,-124.399677?z=9999999",
  48. "geo:-1234,+456?z=01",
  49. "geo:-123456?z=01",
  50. "geo:1੨,-3๔5"
  51. };
  52.  
  53. for (String prueba: pruebas)
  54. {
  55. parsearGeoURI(prueba);
  56. }
  57. }
  58. }
Success #stdin #stdout 0.04s 4386816KB
stdin
Standard input is empty
stdout
URI:      geo:79.786971,-124.399677
Latitud:  79.786971
Longitud: -124.399677
Zoom:     no tiene

URI:      geo:79.786971,-124.399677?z=99
Latitud:  79.786971
Longitud: -124.399677
Zoom:     99

URI:      geo:79.786971,-124.399677?z=9999999
 no es una geo URI

URI:      geo:-1234,+456?z=01
Latitud:  -1234
Longitud: +456
Zoom:     01

URI:      geo:-123456?z=01
 no es una geo URI

URI:      geo:1੨,-3๔5
 no es una geo URI