Home > front end >  Regex with conditional lookbehind on multiple lines
Regex with conditional lookbehind on multiple lines

Time:08-20

I'm looking to update a JSON file for my game that adds a space at the end of dialog to allow for better word wrapping. The problem is that i'm not sure what the syntax is for getting only the dialog and not other lines. Dialog is noted by anything that has code: 401 in it's block.
Is there a way to make a regex add a space at the end of the parameter field but ONLY if the value of code proceeding it is 401?
Right now my regex of [^];]\"\] captures unwanted lines in the example below such as "old event" and "Clear all weathers".

Thanks.

"list": [{         "code": 108,
                   "indent": 0,
                   "parameters": ["old event"]
                   },{
                   "code": 101,
                    "indent": 0,
                    "parameters": ["PlayerFaces", 2, 0, 2]
                }, {
                    "code": 401,
                    "indent": 0,
                    "parameters": ["Are you sure you’re ok?"]
                 }, {
                    "code": 356,
                    "indent": 0,
                    "parameters": ["clear_all_weathers"]
                    }, {
                        "code": 401,
                        "indent": 0,
                        "parameters": ["That was close."]
                    }, {
                        "code": 101,
                        "indent": 0,
                        "parameters": ["GirlFaces", 1, 0, 0]
                    }, {
                        "code": 401,
                        "indent": 0,
                        "parameters": ["What are you talking about? I could of"]
                    }, {
                        "code": 401,
                        "indent": 0,
                        "parameters": ["done that with my eyes closed."]
                        ...

CodePudding user response:

You might be better off with JSONPath. You can specify a path to your data, somehow like xpath. You can try it online at http://jsonpath.com/ and there are implementations for many languages, not just javascript.

With your test data

{
  "list": [
    {
      "code": 108,
      "indent": 0,
      "parameters": ["old event"]
    },
    {
      "code": 101,
      "indent": 0,
      "parameters": ["PlayerFaces", 2, 0, 2]
    },
    {
      "code": 401,
      "indent": 0,
      "parameters": ["Are you sure you’re ok?"]
    },
    {
      "code": 356,
      "indent": 0,
      "parameters": ["clear_all_weathers"]
    },
    {
      "code": 401,
      "indent": 0,
      "parameters": ["That was close."]
    },
    {
      "code": 101,
      "indent": 0,
      "parameters": ["GirlFaces", 1, 0, 0]
    },
    {
      "code": 401,
      "indent": 0,
      "parameters": ["What are you talking about? I could of"]
    },
    {
      "code": 401,
      "indent": 0,
      "parameters": ["done that with my eyes closed."]
    }
  ]
}

you can use the JSONPath $.list.[?(@.code==401)] to filter out only the json elements that contain the code equal to 401.

  •  Tags:  
  • json
  • Related