如何使用regex将JSON对象与唯一ID匹配

时间:2022-01-10 09:38:46

I have the following JSON object:

我有以下JSON对象:

{ 
  "fieldId": "id1", 
  "isVisibleInDetails": true, 
  "isVisibleInGrid": true, 
  "columnWidth": 142, 
}, 
{ 
  "fieldId": "id2", 
  "isVisibleInDetails": true, 
  "isVisibleInGrid": true, 
  "columnWidth": 100, 
}, 
{ 
  "fieldId": "id3", 
  "isVisibleInDetails": true, 
  "isVisibleInGrid": true, 
  "columnWidth": 159, 
}

I want to match the following:

我想匹配以下内容:

{ 
  "fieldId": "id2", 
  "isVisibleInDetails": true, 
  "isVisibleInGrid": true, 
  "columnWidth": 100, 
}, 

I've tried using the regex (\{.*)id2(.*?)\}, but the match returns as:

我尝试过使用regex ({.*)id2(.*?)\},但是匹配返回如下:

{ 
  "fieldId": "id1", 
  "isVisibleInDetails": true, 
  "isVisibleInGrid": true, 
  "columnWidth": 142, 
}, 
{ 
  "fieldId": "id2", 
  "isVisibleInDetails": true, 
  "isVisibleInGrid": true, 
  "columnWidth": 100, 
}, 

Can anyone please correct my regex?

谁能纠正我的正则表达式吗?

3 个解决方案

#1


3  

use this regular expression \{[^{]*id2[^}]*?\}

使用这个正则表达式\[^ { {]* id2[^ }]* ? \ }

#2


0  

I think that knowing what you are trying to do is a very important question, but at a minimum you can use this regex to get exactly what you are looking for:

我认为知道你想做什么是一个非常重要的问题,但至少你可以使用这个regex来得到你想要的:

\{[^}]+?id2.*?\}

#3


0  

You were close..

你是亲密的. .

\{[^{}]*id2[^{}]*\}

use [^{}]* instead of .*?

使用[^ { }]*代替。* ?

OR

 \{(?=[^{}]*id2).*?\}

in your regex . would also match {,} and so it would capture others..use [^{}] to avoid matching {,}

在你的正则表达式。也会匹配{,},因此它会捕获其他的。使用[^ { })来避免匹配{ },

#1


3  

use this regular expression \{[^{]*id2[^}]*?\}

使用这个正则表达式\[^ { {]* id2[^ }]* ? \ }

#2


0  

I think that knowing what you are trying to do is a very important question, but at a minimum you can use this regex to get exactly what you are looking for:

我认为知道你想做什么是一个非常重要的问题,但至少你可以使用这个regex来得到你想要的:

\{[^}]+?id2.*?\}

#3


0  

You were close..

你是亲密的. .

\{[^{}]*id2[^{}]*\}

use [^{}]* instead of .*?

使用[^ { }]*代替。* ?

OR

 \{(?=[^{}]*id2).*?\}

in your regex . would also match {,} and so it would capture others..use [^{}] to avoid matching {,}

在你的正则表达式。也会匹配{,},因此它会捕获其他的。使用[^ { })来避免匹配{ },