为什么我的正则表达式代码没有得到字符串的这一部分?

Why doesn't my regex code get this part of the string?

我正在制作一个应用程序,可以向用户显示最近的健身房。在我向 Google 地点 Api 提出请求后,它 returns 我 json。我使用正则表达式从 json 中获取纬度。但是,我无法获得经度。我的代码如下。有人可以帮忙吗?

 for (int i = 0; i < jsonArray.length(); i++){

                String latitude = "\"lat\":";
                String longitude = "lng\":";
                Pattern p = Pattern.compile(latitude +"(.*?),");
                Pattern pattern = Pattern.compile(longitude + "(.*?)"+ ",");

                JSONObject jsonPart = jsonArray.getJSONObject(i);



                String latLong = jsonPart.getString("geometry");
               Matcher m = p.matcher(jsonPart.getString("geometry"));
                Matcher matcher = pattern.matcher(jsonPart.getString("geometry"));
               while( matcher.find()) {
                   Log.i("longitude", matcher.group(1));

               }
                while (m.find()){

                    Log.i("latitude", m.group(1));
                }






            }

这是我需要从中获取纬度和经度的线。我可以得到纬度,但是经度不行。

gymlocation: {"location":{"lat":40.4434584,"lng":-79.964227}}
String longitude = "\"lng\":";

你不见了\"

或者,这样不是更好吗?

Geting json response from Google places API in android

如果您仍想使用正则表达式,您的第二个表达式 "lng"(.*?), 由于 ? 惰性运算符而找不到任何内容。它会尝试尽可能匹配至少一个字符,并且由于您正在搜索的字符串没有 , 但您的表达式试图在末尾匹配一个字符,所以它不会匹配任何内容。