Community
    • Login

    Replace & add specific parts/symbols from a specific lines only

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    3 Posts 3 Posters 248 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • FlammableF
      Flammable
      last edited by

      Hey guys,
      I’m having a HUGE .json file with 8,000+ lines but everything repeats with the following template.

      "water": {
          "DisplayName": "Water",
          "Skin": 0,
          "Image": "water.png",
          "DefaultAmount": 1,
          "BlockAmountChange": false,
          "BuyPrice": 5000,
          "SellPrice": 0,
          "Currency": "eco"
        },
        "healingtea.advanced": {
          "DisplayName": "default",
          "Skin": 0,
          "Image": "healingtea.advanced.png",
          "DefaultAmount": 1,
          "BlockAmountChange": false,
          "BuyPrice": 500,
          "SellPrice": 0,
          "Currency": "eco"
        },
      

      What I’m trying to do is adding a the same link before the "imagename.png" on every Image line as it’s shown in the current example

      "Image": "https://www.example.com/example2/healingtea.advanced.png"
      

      but also I have to replace the dot . with a dash - for example healingtea-advanced.png
      without changing the dot before the “png” or the added link will break.

      Any help? For the URL part I think I have to use ^"Image": " in the find field with a regex value mode and then in the replace field to add the https://www.example.com/example2/
      Or will this add it in front of ^"Image": " instead properly before the imagename.png?
      But first I have to somehow do the . with - replacing before I add the link and make it more hard for replacing, right?

      All of the lines I have to change starts with ^"Image": " so they can be all searched properly and it’s maybe easy to exclude the . before the png because it’s all the same again.

      I hope I explained everything properly, if you have ideas share them please. I’m completely new here and yesterday learned how to use ^ symbol and Regex myself to add the same " symbol in front of each Notepad++ line and ", at the end but this is more complex for my brain, haha.

      1 Reply Last reply Reply Quote 1
      • Mark OlsonM
        Mark Olson
        last edited by

        @Flammable

        This is the kind of job that you don’t want to use regular expressions for; JSON can be very hard to parse with regex. Use a JSON parser instead.

        If I understand your goal correctly, your JSON document is an object that has many instances of the following:

            "this.is_the.key": {
                "BlockAmountChange": (true or false),
                "BuyPrice": (integer),
                "Currency": (text),
                "DefaultAmount": (integer),
                "DisplayName": (text),
                "Image": (text),
                "SellPrice": (integer),
                "Skin": (integer)
            }
        

        and you want to modify every one of these objects to the following:

            "this.is_the.key": {
                "BlockAmountChange": (true or false),
                "BuyPrice": (integer),
                "Currency": (text),
                "DefaultAmount": (integer),
                "DisplayName": (text),
                "Image": "https://www.example.com/example2/this-is_the-key.png",
                "SellPrice": (integer),
                "Skin": (integer)
            }
        

        There are many ways to do this, including with scripting plugins like PythonScript. I’m going to demonstrate how to do this with the JsonTools plugin using RemesPath. If you need to automate this procedure, I recommend learning Python, and using the steps below as a starting point for your Python script that does this.

        1. Copy the text of your file into a new buffer so that you can compare the result to the original
        2. Open the tree view in JsonTools in your new buffer
        3. Paste the following query in the text box at the top of the tree view
          for x = keys(@);
          at(@, x).Image = `https://www.example.com/example2/` + s_sub(x, `.`, `-`) + `.png`
          
        4. Click the Submit query button; this will change every Image field
        5. You can use ComparePlus to compare the result with the original and make sure it’s to your liking.

        I tested this query with the following START JSON:

        {
            "3kkQI.k67920": {
                "BlockAmountChange": false,
                "BuyPrice": -708951,
                "Currency": "DBg0",
                "DefaultAmount": -618002,
                "DisplayName": "default",
                "Image": "PAC",
                "SellPrice": -191929,
                "Skin": 439298
            },
            "ruHOa": {
                "BlockAmountChange": false,
                "BuyPrice": 800735,
                "Currency": "7",
                "DefaultAmount": 92053,
                "DisplayName": "8G",
                "Image": "cDf.Sdw",
                "SellPrice": 434145,
                "Skin": -127422
            },
            "Eup8FI8t.bvBR_JZcEv.YHBfpk": {
                "BlockAmountChange": true,
                "BuyPrice": 74371,
                "Currency": "KD",
                "DefaultAmount": 687624,
                "DisplayName": "I8v",
                "Image": "I3JGDMP.QUxCfddTP",
                "SellPrice": 94973,
                "Skin": -40263
            },
            "water": {
                "DisplayName": "Water",
                "Skin": 0,
                "Image": "water.png",
                "DefaultAmount": 1,
                "BlockAmountChange": false,
                "BuyPrice": 5000,
                "SellPrice": 0,
                "Currency": "eco"
            },
            "healingtea.advanced": {
                "DisplayName": "default",
                "Skin": 0,
                "Image": "healingtea.advanced.png",
                "DefaultAmount": 1,
                "BlockAmountChange": false,
                "BuyPrice": 500,
                "SellPrice": 0,
                "Currency": "eco"
            }
        }
        

        and got this END JSON:

        {
            "3kkQI.k67920": {
                "BlockAmountChange": false,
                "BuyPrice": -708951,
                "Currency": "DBg0",
                "DefaultAmount": -618002,
                "DisplayName": "default",
                "Image": "https://www.example.com/example2/3kkQI-k67920.png",
                "SellPrice": -191929,
                "Skin": 439298
            },
            "ruHOa": {
                "BlockAmountChange": false,
                "BuyPrice": 800735,
                "Currency": "7",
                "DefaultAmount": 92053,
                "DisplayName": "8G",
                "Image": "https://www.example.com/example2/ruHOa.png",
                "SellPrice": 434145,
                "Skin": -127422
            },
            "Eup8FI8t.bvBR_JZcEv.YHBfpk": {
                "BlockAmountChange": true,
                "BuyPrice": 74371,
                "Currency": "KD",
                "DefaultAmount": 687624,
                "DisplayName": "I8v",
                "Image": "https://www.example.com/example2/Eup8FI8t-bvBR_JZcEv-YHBfpk.png",
                "SellPrice": 94973,
                "Skin": -40263
            },
            "water": {
                "DisplayName": "Water",
                "Skin": 0,
                "Image": "https://www.example.com/example2/water.png",
                "DefaultAmount": 1,
                "BlockAmountChange": false,
                "BuyPrice": 5000,
                "SellPrice": 0,
                "Currency": "eco"
            },
            "healingtea.advanced": {
                "DisplayName": "default",
                "Skin": 0,
                "Image": "https://www.example.com/example2/healingtea-advanced.png",
                "DefaultAmount": 1,
                "BlockAmountChange": false,
                "BuyPrice": 500,
                "SellPrice": 0,
                "Currency": "eco"
            }
        }
        
        1 Reply Last reply Reply Quote 2
        • guy038G
          guy038
          last edited by guy038

          Hello, @flammable and All,

          First, I would advice you to fully read this interesting FAQ post below, which explains to prefer the use of a JSON parser ( instead of regexes ) for such problems :

          https://community.notepad-plus-plus.org/topic/25304/faq-parsing-and-editing-json-with-regex-is-a-bad-idea/1?lang=fr

          Oh,…, by moving to your post, I just see the @mark-olson’s reply which explains a straight solution, based on the JsonTools plugin ;-))


          However, I also succeeded to find out a regex way to do it !

          So, given your INPUT text, below, in a new tab :

          "water": {
              "DisplayName": "Water",
              "Skin": 0,
              "Image": "water.png",
              "DefaultAmount": 1,
              "BlockAmountChange": false,
              "BuyPrice": 5000,
              "SellPrice": 0,
              "Currency": "eco"
            },
            "healingtea.advanced": {
              "DisplayName": "default",
              "Skin": 0,
              "Image": "healingtea.advanced.png",
              "DefaultAmount": 1,
              "BlockAmountChange": false,
              "BuyPrice": 500,
              "SellPrice": 0,
              "Currency": "eco"
            },
          
          • Open the Replace dialog ( Ctrl + H )

          • Untick all box options

          • SEARCH (?-is)^\x20+"Image":\x20"\K

          • REPLACE https://www.example\.com/example2/

          • Check the Wrap around option

          • Click, once, on the Replace All button ( NOT the Replace button ! )

          You should get this OUTPUT text :

          "water": {
              "DisplayName": "Water",
              "Skin": 0,
              "Image": "https://www.example.com/example2/water.png",
              "DefaultAmount": 1,
              "BlockAmountChange": false,
              "BuyPrice": 5000,
              "SellPrice": 0,
              "Currency": "eco"
            },
            "healingtea.advanced": {
              "DisplayName": "default",
              "Skin": 0,
              "Image": "https://www.example.com/example2/healingtea.advanced.png",
              "DefaultAmount": 1,
              "BlockAmountChange": false,
              "BuyPrice": 500,
              "SellPrice": 0,
              "Currency": "eco"
            },
          

          Now with the following regex S/R, we’ll change any dot character, not followed by the string png", at end of line, with a dash char :

          SEARCH (?-si)(?:^\x20+"Image":\x20"https://www.example.com/example2/|(?!\A)\G).*?\K\.(?!png",$)

          REPLACE -

          Just follow, exactly, the same other points of the previous S/R !

          You should get your expected OUTPUT text :

          "water": {
              "DisplayName": "Water",
              "Skin": 0,
              "Image": "https://www.example.com/example2/water.png",
              "DefaultAmount": 1,
              "BlockAmountChange": false,
              "BuyPrice": 5000,
              "SellPrice": 0,
              "Currency": "eco"
            },
            "healingtea.advanced": {
              "DisplayName": "default",
              "Skin": 0,
              "Image": "https://www.example.com/example2/healingtea-advanced.png",
              "DefaultAmount": 1,
              "BlockAmountChange": false,
              "BuyPrice": 500,
              "SellPrice": 0,
              "Currency": "eco"
            },
          

          BR

          guy038

          1 Reply Last reply Reply Quote 1
          • First post
            Last post
          The Community of users of the Notepad++ text editor.
          Powered by NodeBB | Contributors