2015-10-24 18 views
-1

我有一個JSON文件是這樣的:匹配值的(最後),其中附有支架 - 正則表達式

{ 
      "_id" : ObjectId("5627ffddce2790eea0d96ba4"), 
      "type" : "FeatureCollection", 
      "crs" : { 
       "type" : "name", 
       "properties" : { 
        "name" : "urn:ogc:def:crs:OGC:1.3:CRS84" 
       } 
      }, 
      "features" : { 
       "type" : "Feature", 
       "properties" : { 
        "id" : 17094.0000000000000000, 
        "osm_id" : 311636347.0000000000000000, 
        "name" : "King Charles Court", 
        "type" : "apartments" 
       }, 
       "geometry" : { 
        "type" : "MultiPolygon", 
        "coordinates" : [ 
         [ 
          [ 
           [ 
            -123.1346724048378600, 
            49.2897742781884180 
           ], 
           [ 
            -123.1345008272799100, 
            49.2898879367954520 
           ], 
           [ 
            -123.1343453429760300, 
            49.2897882759667140 
           ], 
           [ 
            -123.1345169205340000, 
            49.2896744497216160 
           ], 
           [ 
            -123.1346724048378600, 
            49.2897742781884180 
           ] 
          ] 
         ] 
        ] 
       }  # <-- I want to find this # 
      } 
     }, 
     { 
      "_id" : ObjectId("5627ffddce2790eea0d96ba4"), 
      "type" : "FeatureCollection", 
      "crs" : { 
       "type" : "name", 
       "properties" : { 
        "name" : "urn:ogc:def:crs:OGC:1.3:CRS84" 
       } 
      }, 
      "features" : { 
       "type" : "Feature", 
       "properties" : { 
        "id" : 17123.0000000000000000, 
        "osm_id" : 311859620.0000000000000000, 
        "name" : "The Burkingham", 
        "type" : "apartments" 
       }, 
       "geometry" : { 
        "type" : "MultiPolygon", 
        "coordinates" : [ 
         [ 
          [ 
           [ 
            -123.1352148816112600, 
            49.2879125736745320 
           ], 
           [ 
            -123.1351233512286000, 
            49.2879720851870790 
           ], 
           [ 
            -123.1350737303618100, 
            49.2879396472218050 
           ] 
          ] 
         ] 
        ] 
       }  # <-- I want to find this # 
      } 
     } 

我想找出正則表達式,將發現後的第一個右括號(}) 「座標」的第一個方括號()。所以在"coordinates" : [ ...]之後它會匹配第一個}。我跟着some regex tutorials這裏,但是這似乎是我目前的知識有點太複雜......

+2

解析JSON與正則表達式是一個完整的痛苦。你可以使用[ruby](http://stackoverflow.com/questions/5410682/parsing-a-json-string-in-ruby),[node](http://stackoverflow.com/questions/5726729/how- to-parse-json-using-node-js),(以及其他許多)JSON解析器,將您正在查找的數據轉換爲更簡單的格式。 –

+0

同意其他評論在這裏。回過頭來解釋你爲什麼要這麼做。你在這裏有一個容易反序列化的數據結構。爲什麼不只是反序列化呢? –

+0

我試圖通過Java來完成這項工作,但自從我的文件太大以來我一直很難。這就是爲什麼我想出了** regex **,我可以直接從'notepad ++'運行。 –

回答

1

這裏是您所描述的正則表達式:

"coordinates"[^]]*][^}]*\} 

然而,在你的榜樣,第一閉幕後的括號"coordinates"後第一右方括號也"coordinates"後的第一個大括號,這使得正則表達式更簡單:

"coordinates"[^}]*\} 

如果要匹配只有支架,前右其添加\K

"coordinates"[^}]*\K\} 

\K的意思是「假裝真的在這裏開始了比賽。」

+0

令人難以置信。謝謝! –