Community
    • Login

    Help required to remove unwanted strings of text

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    7 Posts 3 Posters 2.0k Views 2 Watching
    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.
    • Muslim FarmerM Offline
      Muslim Farmer
      last edited by Muslim Farmer

      I need to be able to delete all text between { } that do not contain ‘XboxOfficial’, would like to delete the bottom text, but keep the top. This is for over 10000 lines of code.

      https://gyazo.com/5e91401bb101e72ef44e14e59e0b8b82

      1 Reply Last reply Reply Quote 0
      • guy038G Offline
        guy038
        last edited by guy038

        Hello, @muslim-farmer,

        This could be probably achieved with regular expressions ! But I need additional information :

        Let’s imagine a {........} block which does not contain the word XboxOfficial, as below :

        },{
        "ClusterId": "NewXboxPVP",
        "IP": "37.10.124.94",
        "Name": "NA-PVP-XBlaBlahOfficial-Ragnarok495",
        "Port": 9990,
        },{
        

        Two questions :

        • Firstly, is the word XboxOfficial always located after the “Name”: statement ?

        • Secondly, do you expect the replaced text to be :

        A) All that bloc is deleted :

        },{
        

        B) Only the lines, after the line containing a name different from XboxOfficial, till the ending boundary }, are deleted :

        },{
        "ClusterId": "NewXboxPVP",
        "IP": "37.10.124.94",
        "Name": "NA-PVP-XBlablahOfficial-Ragnarok495",
        },{
        

        C) Only the lines, including the line containing a name different from XboxOfficial, till the ending boundary }, are deleted :

        },{
        "ClusterId": "NewXboxPVP",
        "IP": "37.10.124.94",
        },{
        

        Best Regards,

        guy038

        Muslim FarmerM 1 Reply Last reply Reply Quote 3
        • Muslim FarmerM Offline
          Muslim Farmer @guy038
          last edited by Muslim Farmer

          @guy038 I desire the whole block to be deleted if it is not present.

          1 Reply Last reply Reply Quote 0
          • guy038G Offline
            guy038
            last edited by guy038

            Hello, @muslim-farmer and All,

            EDIT : Have also a look to my next post for a more correct syntax of the search regex !

            OK ! So, assuming the sample text, below :

            },{
            "ClusterId": "NewXboxPVP",
            "IP": "37.10.124.94",
            "Name": "NA-PVP-XboxOfficial-Ragnarok495",
            "Port": 9990,
            },{
            "ClusterId": "NewXboxPVP",
            "IP": "37.10.124.94",
            "Name": "NA-PVP-XBlablaOfficial-Ragnarok495",
            "Port": 9990,
            },{
            "ClusterId": "NewXboxPVP",
            "IP": "37.10.124.94",
            "Name": "NA-PVP-XBlablaOfficial-Ragnarok495",
            "Port": 9990,
            },{
            "ClusterId": "NewXboxPVP",
            "IP": "37.10.124.94",
            "Name": "NA-PVP-XboxOfficial-Ragnarok495",
            "Port": 9990,
            },{
            "ClusterId": "NewXboxPVP",
            "IP": "37.10.124.94",
            "Name": "NA-PVP-An OTHER value-Ragnarok495",
            "Port": 9990,
            },{
            "ClusterId": "NewXboxPVP",
            "IP": "37.10.124.94",
            "Name": "NA-PVP-XboxOfficial-Ragnarok495",
            "Port": 9990,
            },{
            
            • Open the Replace dialog ( Ctrl + H )

            • SEARCH (?s)\{((?!XboxOfficial).)+?\}

            • REPLACE Leave EMPTY

            • Tick, preferably, the Wrap around option

            • Select the regular expression search mode

            • Click, once on the Replace All button or several times on the Replace button

            You should get the expected text :

            },{
            "ClusterId": "NewXboxPVP",
            "IP": "37.10.124.94",
            "Name": "NA-PVP-XboxOfficial-Ragnarok495",
            "Port": 9990,
            },,,{
            "ClusterId": "NewXboxPVP",
            "IP": "37.10.124.94",
            "Name": "NA-PVP-XboxOfficial-Ragnarok495",
            "Port": 9990,
            },,{
            "ClusterId": "NewXboxPVP",
            "IP": "37.10.124.94",
            "Name": "NA-PVP-XboxOfficial-Ragnarok495",
            "Port": 9990,
            },{
            

            All the blocks which contain, in the Name part, an other string than XboxOfficial are totally deleted ;-))

            If my first attempt is the good one, I’ll explain to you how the regex works, next time !

            Best Regards,

            guy038

            1 Reply Last reply Reply Quote 3
            • fotofrankF Offline
              fotofrank
              last edited by

              I think this RegEx will do the trick:

              \{(?!([^\{\}]*(\b)(XboxOfficial|Xbox360)(\b)[^\{\}]*)).*?\}\, 
              

              It uses a “keyword”-list for whether or not a block of code will be removed. In the RegEx this is the part: (XboxOfficial|Xbox360)
              Between the parenthesis you can add/remove keywords, as long as they are separated by a | character (pipeline).

              • Open the Find/Replace
              • Select the tab: Mark
              • Copy the RegEx into Find what:
              • Look at the images for the other settings to make this work!
              • Click on Mark All and NPP will highlight the text matched by the RegEx.
              • Especially when you are using the RegEx for the first time, verify that all the highlighted text is indeed the part you want to remove!
              • If so: Click on the Tab: Replace
              • Find what must still contain the RegEx.
              • Leave Replace with: EMPTY
              • You can click on the Replace button a few times to check if the highlighted text is being removed. When this works then you can click on Replace All.
              • When NPP is done removing the rest, there should be no highlighted text anymore.

              3 blocks marked
              3 blocks deleted

              4 blocks marked
              4 blocks deleted

              The samples as text code:

              The sample text that I've used
              
              
              },{
              "ClusterId": "NewXboxPVP",
              "IP": "37.10.124.94",
              "Name": "NA-PVP-XboxOfficial-Ragnarok495",
              "Port": 0001,
              },{
              "ClusterId": "NewXboxPVP",
              "IP": "37.10.124.94",
              "Name": "NA-PVP-PlaystationOfficial-Ragnarok495",
              "Port": 0002,
              },{
              "ClusterId": "NewXboxPVP",
              "IP": "37.10.124.94",
              "Name": "NA-PVP-XboxUNOfficial-Ragnarok495",
              "Port": 0003,
              },{
              "ClusterId": "NewXboxPVP",
              "IP": "37.10.124.94",
              "Name": "NA-PVP-XboxOfficial-Ragnarok",
              "Port": 0004,
              },{
              "ClusterId": "NewXboxPVP",
              "IP": "37.10.124.94",
              "Name": "NA-PVP-XboxOfficial-Ragnarok495",
              "Port": 0005,
              },{
              "ClusterId": "NewXboxPVP",
              "IP": "37.10.124.94",
              "Name": "NA-PVP-Xbox360-Ragnarok495",
              "Port": 0006,
              },{
              "ClusterId": "NewXboxPVP",
              "IP": "37.10.124.94",
              "Name": "NA-PVP-XboxOfficial-Frank123",
              "Port": 0007,
              },{
              "ClusterId": "NewXboxPVP",
              "IP": "37.10.124.94",
              "Name": "NA-PVP-XboxOfficial-Ragnarok495",
              "Port": 0008,
              },{
              "ClusterId": "NewXboxPVP",
              "IP": "37.10.124.94",
              "Name": "NA-PVP-C64Official-Ragnarok495",
              "Port": 0009,
              },{
              

              .

              With 1 Keyword matching (XboxOfficial|ZXSpectrum)
              
              
              },{
              "ClusterId": "NewXboxPVP",
              "IP": "37.10.124.94",
              "Name": "NA-PVP-XboxOfficial-Ragnarok495",
              "Port": 0001,
              },{
              "ClusterId": "NewXboxPVP",
              "IP": "37.10.124.94",
              "Name": "NA-PVP-XboxOfficial-Ragnarok",
              "Port": 0004,
              },{
              "ClusterId": "NewXboxPVP",
              "IP": "37.10.124.94",
              "Name": "NA-PVP-XboxOfficial-Ragnarok495",
              "Port": 0005,
              },{
              "ClusterId": "NewXboxPVP",
              "IP": "37.10.124.94",
              "Name": "NA-PVP-XboxOfficial-Frank123",
              "Port": 0007,
              },{
              "ClusterId": "NewXboxPVP",
              "IP": "37.10.124.94",
              "Name": "NA-PVP-XboxOfficial-Ragnarok495",
              "Port": 0008,
              },{
              

              .

              With 2 Keywords matching (XboxOfficial|Xbox360)
              
              
              },{
              "ClusterId": "NewXboxPVP",
              "IP": "37.10.124.94",
              "Name": "NA-PVP-XboxOfficial-Ragnarok495",
              "Port": 0001,
              },{
              "ClusterId": "NewXboxPVP",
              "IP": "37.10.124.94",
              "Name": "NA-PVP-XboxOfficial-Ragnarok",
              "Port": 0004,
              },{
              "ClusterId": "NewXboxPVP",
              "IP": "37.10.124.94",
              "Name": "NA-PVP-XboxOfficial-Ragnarok495",
              "Port": 0005,
              },{
              "ClusterId": "NewXboxPVP",
              "IP": "37.10.124.94",
              "Name": "NA-PVP-Xbox360-Ragnarok495",
              "Port": 0006,
              },{
              "ClusterId": "NewXboxPVP",
              "IP": "37.10.124.94",
              "Name": "NA-PVP-XboxOfficial-Frank123",
              "Port": 0007,
              },{
              "ClusterId": "NewXboxPVP",
              "IP": "37.10.124.94",
              "Name": "NA-PVP-XboxOfficial-Ragnarok495",
              "Port": 0008,
              },{
              

              Important!
              Each block of text between the { and }, that contains a word from the RegEx word-list will NOT be selected for removal.

              (This is my first post on the NPP Community so forgive me for when I’ve done something “not correctly” according to the rules.)

              1 Reply Last reply Reply Quote 3
              • guy038G Offline
                guy038
                last edited by guy038

                Hello, @muslim-farmer, @fotofrank and All,

                @fotofrank, welcome to the Notepad++ Community !

                Studying your version of the search regex and looking at the results after replacement, of my previous post, I just realized that I omitted to get rid of the comma, in the string },{

                Of course, the correct version of my regex is :

                (?s)\{((?!XboxOfficial).)+?\},

                Leading to these results :

                },{
                "ClusterId": "NewXboxPVP",
                "IP": "37.10.124.94",
                "Name": "NA-PVP-XboxOfficial-Ragnarok495",
                "Port": 9990,
                },{
                "ClusterId": "NewXboxPVP",
                "IP": "37.10.124.94",
                "Name": "NA-PVP-XboxOfficial-Ragnarok495",
                "Port": 9990,
                },{
                "ClusterId": "NewXboxPVP",
                "IP": "37.10.124.94",
                "Name": "NA-PVP-XboxOfficial-Ragnarok495",
                "Port": 9990,
                },{
                

                Cheers,

                guy038

                P.S. :

                @fotofrank, in order that your regex works correctly, you must, either :

                • Tick the . matches newline option of the Replace dialog

                • Add, at a location before the dot meta-character, the in-line modifier (?s) which means that any further dot ( . ) will match, absolutely, any single character ( EOL or standard ones )

                So, one of the two regexes, below :

                SEARCH (?s)\{(?!([^\{\}]*\b(XboxOfficial|Xbox360)\b[^\{\}]*)).*?\},

                SEARCH \{(?!([^\{\}]*\b(XboxOfficial|Xbox360)\b[^\{\}]*))(?s).*?\},

                1 Reply Last reply Reply Quote 3
                • fotofrankF Offline
                  fotofrank
                  last edited by fotofrank

                  @guy038,

                  Thanks!
                  Though I’m using Regular Expressions a LOT, I’m always forgetting about the in-line modifiers.

                  Greetz,
                  fotofrank

                  1 Reply Last reply Reply Quote 1

                  Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                  Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                  With your input, this post could be even better 💗

                  Register Login
                  • First post
                    Last post
                  The Community of users of the Notepad++ text editor.
                  Powered by NodeBB | Contributors