匹配值的(最后)封闭括号 - 正则表达式

时间:2015-10-24 01:45:21

标签: regex notepad++

我有一个像这样的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在这里,但这对我目前的知识来说似乎有点过于复杂......

1 个答案:

答案 0 :(得分:1)

这是你描述的正则表达式:

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

但是,在您的示例中, "coordinates" 之后的第一个右方括号之后的第一个右大括号也是"coordinates"之后的第一个右大括号,这使得正则表达式均匀更简单的:

"coordinates"[^}]*\}

如果您只想匹配 大括号,请在它前面添加\K

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

\K表示&#34;假装比赛真正在这里开始&#34;。