Regex 如何使用正则表达式将JSON对象与唯一ID匹配
我有以下JSON对象:Regex 如何使用正则表达式将JSON对象与唯一ID匹配,regex,json,match,Regex,Json,Match,我有以下JSON对象: { "fieldId": "id1", "isVisibleInDetails": true, "isVisibleInGrid": true, "columnWidth": 142, }, { "fieldId": "id2", "isVisibleInDetails": true, "isVisibleInGrid": true, "columnWidth": 100, }, { "fieldId": "i
{
"fieldId": "id1",
"isVisibleInDetails": true,
"isVisibleInGrid": true,
"columnWidth": 142,
},
{
"fieldId": "id2",
"isVisibleInDetails": true,
"isVisibleInGrid": true,
"columnWidth": 100,
},
{
"fieldId": "id3",
"isVisibleInDetails": true,
"isVisibleInGrid": true,
"columnWidth": 159,
}
我想匹配以下内容:
{
"fieldId": "id2",
"isVisibleInDetails": true,
"isVisibleInGrid": true,
"columnWidth": 100,
},
我已经尝试使用regex(\{.*)id2(.*?\},
,但是匹配返回为:
{
"fieldId": "id1",
"isVisibleInDetails": true,
"isVisibleInGrid": true,
"columnWidth": 142,
},
{
"fieldId": "id2",
"isVisibleInDetails": true,
"isVisibleInGrid": true,
"columnWidth": 100,
},
有人能纠正我的正则表达式吗?使用这个正则表达式
\{[^{]*id2[^}]*?\}
我认为知道你想做什么是一个非常重要的问题,但至少你可以使用这个正则表达式来获得你想要的东西:
\{[^}]+?id2.*?\}
你很接近
\{[^{}]*id2[^{}]*\}
使用[^{}]*
而不是*?
或
\{(?=[^{}]*id2).*?\}
在您的正则表达式中,
也将匹配{
,}
,因此它将捕获其他对象。使用[^{}]
避免匹配{
,}需要注意的是,它引入第一个对象的原因是*
是贪婪的,并且会获取匹配的所有可能对象,既然有一个“{”在第一个对象的开头,它将其包含在搜索中。您应该真正使用JSON解析器来操作/导航JSONI。我将使用此正则表达式来匹配oracle数据库中的一些JSON,使用PL Sqlworth。可能值得指出的是,这些是JSON片段,但不是有效的JSON文档。JSON解析器没有义务解析片段。要如果是有效的JSON文档,则必须有根对象(可以是对象或arra)。您有一系列没有根的对象。