Search code examples
regexreplacegroupingvariable-substitution

Capture and replace a string found between two strings


I am trying to replace the values for a specific variable named variable but only if this is contained between two strings, namely between group1 and group2. (or group1 till the end of text if group2 isn't found)

variable can also be found between group2 and group1 but should not be touched!

A picture is better than a thousand words so here's what I am trying to achieve: variable found in the blue highlighted zone need to be individually matched. other variable need to be ignored, and left as it is. enter image description here

I got two regex but cannot find how to make them work together in one regex...

  • group1(.*?)group2 shows me the text between group1 and group2
  • \"variable\":\d{1,2} shows me the variable and their value that I need to match

My JSON text is

{"mastercont":{"subcont":368,"whatever":236,"anothergroup":{"ItemsG1":["cf8013791a1948e9ad7ec913353cf108","7093f33afa794316af1a6e43d3e3e42f","38670584b898415e9e2e3b2dee39610c","d4d17e6a40ee4b4187a421ac230b6bb0","053c112d38384fa6bff09c01fa48f10d"],"items":[{"id":"14cec419535e449e9d367cb89dfe9b45","Template":"4fdf0640c1094f6cad1d37c332d01180","var3":10,"var2":3035385,"prop":{"group1":[{"id":"e4d9f3206a0d4dd3947a9ab887e2a866","variable":8},{"id":"33f866a5f2f546afac13fa5f02bedfbf","variable":8}]}},{"id":"a5c48bf889cd4a609ef32582c034a137","Template":"023bbfbbd15646ce8eb92cad079142fd","var3":10,"var2":2627635,"prop":{"group1":[{"id":"b4de8d1bd8a145758517c1e5800a7525","variable":5}]}},{"id":"4266dd790972412b8202506f82f07bce","Template":"d501df0303664ee798085626d1b5f58a","var3":10,"var2":2687981,"prop":{"group1":[{"id":"c40ed85187774d09b1690223dae8f67d","variable":7},{"id":"1674a05b355a4c0199947a28e2654d4c","variable":7},{"id":"bf107d7d87774411b07a56819a58a709","variable":7}]}},{"id":"8d9215eb57bd4a9c97c97042602f7385","Template":"0e24274c1f9f4a75b72459237776a6f5","var3":10,"var2":2776081,"prop":{"group1":[{"id":"464bedb7a63143b6a2dff65f089d39da","variable":7}]}},{"id":"d461eb38afc445b4be85c8fd9ca4b61e","Template":"12ab90763c034e628f89f36fbe08bd0c","var3":10,"var2":3991635,"prop":{"group1":[{"id":"848e02b432ae4e1b809c83bca039542a","variable":10}]}}]},"loadout":{"equippedItems":{"48021ab1a1a6487b80a4ca472a4d0c77":{"id":"cf8013791a1948e9ad7ec913353cf108","Template":"4535a4bf490a46d5b49ca9bc0cc81fe6","var3":10,"var2":287004,"prop":{"group1":[{"id":"e4d9f3206a0d4dd3947a9ab887e2a866","variable":10},{"id":"c02cf3392c634456bdd1f4ad1e25bf3f","variable":10},{"id":"e8e0bb2627514cacbd20effdec8dea4d","variable":10}]},"slot":"48021ab1a1a6487b80a4ca472a4d0c77"},"36d141e47783466c95656f90f09de428":{"id":"f68adc253dff47f586ba5229b8ddfe81","Template":"1ca7737c1eef462ab85d922dfb5eb5b6","grade":3,"prop":{"group1":[{"id":"80980773a3884f719ba5ac69a7e307be","variable":9},{"id":"2e56b62015ce4db29eef2faf139f6185","variable":9},{"id":"0717438e41e04414913ca089a60666c0","variable":9}],"group2":[{"id":"ff35b37f7bc3455f81d73a2b60417ab5","variable":1},{"id":"2fc557411c8946cd85e2a8a27f6cea38","variable":1},{"id":"7ed4d49b50534a5dbbacb32a96871a85","variable":1}]},"slot":"36d141e47783466c95656f90f09de428"},"417e79dec81042f88273f9759df6ae25":{"id":"d4d17e6a40ee4b4187a421ac230b6bb0","Template":"9094f0d120c84013ab3877177a009a78","var3":10,"var2":4109,"prop":{"group1":[{"id":"c40ed85187774d09b1690223dae8f67d","variable":7}]},"slot":"417e79dec81042f88273f9759df6ae25"},"897a600c91d64449af09173da88a907e":{"id":"053c112d38384fa6bff09c01fa48f10d","Template":"c74b89cb19cc45299c7eec6b53bcac17","var3":10,"var2":326029,"prop":{"group1":[{"id":"e4d9f3206a0d4dd3947a9ab887e2a866","variable":10}]},"slot":"897a600c91d64449af09173da88a907e"},"862605dec67f4bceb5274e5fb6f25162":{"id":"21e4c1490f0c4c7bbe3b012a51823712","Template":"875aa16e9d2646a0934e6ba5b2a5299f","var3":10,"var2":5044607,"prop":{"group1":[{"id":"b64fa9d44f134bb4912916df08e84757","variable":10},{"id":"a869f868c23f45bb9159d62b42236a4a","variable":10}]},"slot":"862605dec67f4bceb5274e5fb6f25162"},"e273a4d7fb874f7e8f1e398be59afbcb":{"id":"7093f33afa794316af1a6e43d3e3e42f","Template":"b73fa9abfdd049a08b16de3b8a8961fd","var3":10,"var2":287004,"prop":{"group1":[{"id":"e4d9f3206a0d4dd3947a9ab887e2a866","variable":10},{"id":"c02cf3392c634456bdd1f4ad1e25bf3f","variable":10}]},"slot":"e273a4d7fb874f7e8f1e398be59afbcb"},"58b6d1212e234fa4b892c92ae2e2c4c5":{"id":"38670584b898415e9e2e3b2dee39610c","Template":"67b78223916e440b8ec6fc14b83e1150","var3":10,"var2":417904,"prop":{"group1":[{"id":"626121b7708d455fb32f38e5b68bb80c","variable":9}]},"slot":"58b6d1212e234fa4b892c92ae2e2c4c5"},"959c1931bf85458792ec8ecaa58b06d5":{"id":"8019f35c15744377ab99eb0b2ad87ec0","Template":"d408a9123a4f4b2abf4da55b78910f08","grade":5,"prop":{"group1":[{"id":"626121b7708d455fb32f38e5b68bb80c","variable":3},{"id":"8fd72d413e824865b49eb576b8384aa9","variable":3}],"group2":[{"id":"f17be1d91c1e451085143d108b92220f","variable":2},{"id":"0eec28838bc14fdda9398292b545e35d","variable":2},{"id":"d17491225ada4017918d990fc7717481","variable":1}]},"slot":"959c1931bf85458792ec8ecaa58b06d5"},"0d8f2023470141e88bd592381d787456":{"id":"c4fe7d61a70642afb4bd62b28add56cb","Template":"d408a9123a4f4b2abf4da55b78910f08","grade":3,"prop":{"group1":[{"id":"626121b7708d455fb32f38e5b68bb80c","variable":7},{"id":"262ece9bbd654876b6985926ca4422be","variable":7}],"group2":[{"id":"8e9928a847934e15ae2491c41aebad9c","variable":1},{"id":"6d4e443734484a6198e812a7e6850c8c","variable":1},{"id":"6bc195682f764c0e8482dee16629dc5b","variable":1}]},"slot":"0d8f2023470141e88bd592381d787456"}}}}}

Based on the comment below from toto, a working regex is (?:"group1"|\G(?!^))(?:(?!"group2"|"variable").)*("variable":\d*) https://regex101.com/r/5EGTHu/1 But I fail to understand how I can replace variable:xx by variable:10

Any help with the substitution / replace would be greatly appreciated. I tried various input but I only end up adding text to $0 or loosing the whole match.

Note that I unfortunately have to use a regex here, and cannot use a json parser.


Solution

  • I make my comment an answer.

    Use

    • Find: (?:"group1"|\G(?!^))(?:(?!"group2"|"variable").)*"variable":\K\d*
    • Replace: 10

    Demo & explanation

    If \K is not supported, use:

    • Find: ((?:"group1"|\G(?!^))(?:(?!"group2"|"variable").)*"variable":)\d*
    • Replace: ${1}10

    Demo & explanation