I have a string in a JSON file which is repeated with different values for chance. I want to replace all of these blocks by moving the miss chance out of the attack array and into a separate property.
"attacks": [
{
"name": "miss",
"chance": "0.25"
},
...
]
"missChance": 0.25,
"attacks": [
...
]
I managed to write a (basic) search pattern that would find all of these bits of JSON, and had grouping so I would be able to pull out the chance:
/"attacks":\s*[\_s*{\_s*"name":\s*"miss",\_s*"chance":\s"\(0\.\d*\)"\_s*},
However, although this search pattern would find all of the occurrences I wanted to replace, when I use it in a substitute command it returns "Pattern not found". The substitute I was using was:
:%s/"attacks":\s*[\_s*{\_s*"name":\s*"miss",\_s*"chance":\s"\(0\.\d*\)"\_s*},/"missChance": \1^M"attacks": [/g
I have noticed that the error includes more of the search pattern than I expected; it looks like it's searching for everything after the second "/" separator for some reason. Why would the search be different in the substitute command to in a normal search?
This is the error:
Pattern not found: "attacks":\s*[\_s*{\_s*"name":\s*"miss",\_s*"chance":\s"\(0\.\d*\)"\_s*},/"missChance": \1,^M"attacks": [/g
:s///g
, but likely far less complicated to write.