开发者

Vim regex to find missing JSON element

开发者 https://www.devze.com 2023-03-04 02:35 出处:网络
I have a big JSON file, formatted over multiple lines. I want to find objects that don\'t have a given property. The objects are guaranteed not to contain any further nested objects. Say the given pro

I have a big JSON file, formatted over multiple lines. I want to find objects that don't have a given property. The objects are guaranteed not to contain any further nested objects. Say the given property was "bad", then I would want to locate the value of"foo" in the second element in the following (but not in the first element).

{
  result: [
    {
      "foo" : {
        "good" : 1,
  开发者_C百科      "bad" : 0
      },
      "bar" : 123
    },
    {
      "foo" : {
        "good" : 1
      },
      "bar" : 123
    }
  ]
}

I know about multi-line regexes in Vim but I can't get anything that does what I want. Any pointers?


Try the following:

/\v"foo"\_s*:\_s*\{%(%(\_[\t ,]"bad"\_s*:)@!\_.){-}\}

When you need to exclude something, you should look at negative look-aheads or look-behinds (latter is slower and unlike vim Perl/PCRE regular expressions do not support look-behinds except fixed-width (or a number of alternative fixed-width) ones).


JSON is a context free grammar and as such is not regular. Unless you can give a much stricter set of rules to go on, no regex will be able to do what you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号