Community
    • Login

    Multiple word search and replace within a range start with xxx and end with yyy

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    7 Posts 3 Posters 2.5k 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.
    • Wagner LeungW Offline
      Wagner Leung
      last edited by

      I have already read from this and know that I can do Multiple word search and replace within a file by
      Install Python Script plugin from Plugin Manager.
      Create a file with your substitutions (e.g., C:/Temp/Substitutions.txt), separate values with space:

      good bad
      great worse
      fine not
      

      Create a new script:

      with open('C:/Temp/Substitutions.txt') as f:
          for l in f:
              s = l.split()
              editor.replace(s[0], s[1])
      Run the new script against the text you want to substitute.
      

      However, there are multiple objects in the file, and I just want to change the string within certain objects for example:

      object={
      ...
      type=1
      b={
      abc= 123
      def = 456
      ghi=789
      }
      }
      
      object={
      ...
      type=2
      b={
      abc= 987
      bbb = 654
      ccc=321
      }
      }
      
      object={
      ...
      type=3
      b={
      abc= 000
      fed = 001
      ihg=002
      }
      }
      
      object={
      ...
      type=2
      b={
      abc= 987
      bbb = 654
      ccc=321
      }
      }
      

      I just want to change every “abc” from every type 2 object.
      Is it possible to write a script that the program would start replacing at " type=2" and end at “}” for the whole file?

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

        Hello, @wagner-leung and All,

        I can’t give you advices about a Python script solution but this regex S/R seems to work nice :

        • SEARCH (?s)object((?!object).)+?type=2(.+?)abc=\x20\K987

        or

        • SEARCH (?s)object((?!object).)+?type=2(.+?)abc=\x20\K\d+    if you’re looking for any integer value fore abc

        • REPLACE Your Integer !

        • Tick the Wrap around option

        • Select the Regular expression search mode

        • Click on the Replace All button

        Best Regards,

        guy038

        1 Reply Last reply Reply Quote 0
        • Wagner LeungW Offline
          Wagner Leung
          last edited by

          abc is string

          @guy038 said in Multiple word search and replace within a range start with xxx and end with yyy:

          REPLACE Your Integer !

          Alan KilbornA 1 Reply Last reply Reply Quote 0
          • Alan KilbornA Offline
            Alan Kilborn @Wagner Leung
            last edited by

            @Wagner-Leung

            So it is a very common problem here that people don’t provide BEFORE and AFTER data examples.

            @guy038 assumed that you were interested in the number after abc= when it appears (I think) that you are more interested in the abc itself.

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

              Hi, @wagner-leung and All,

              Ah OK ! So the variable abc contains the string 123. However my reasoning remains identical :

              I supposed that you want to change the string 987 of the variable abc, when inside an object block, containing the line Type = 2, by any personal string, inserted in the Replace zone !

              A better regex, which catches any value of the variable abc, would be :

              SEARCH (?s)object((?!object).)+?type=2.+?abc=\x20\K.+?$

              May be, the best would be to show us a piece of your initial text and your final expected text ! to verify if we’re speaking about the same thing :-)

              See you later !

              BR

              guy038

              Wagner LeungW 1 Reply Last reply Reply Quote 1
              • Wagner LeungW Offline
                Wagner Leung @guy038
                last edited by

                @guy038
                I want to change the variable abc but not the string.
                As this is another coding language string integer does not make any difference.

                Here is the intended result

                object={
                ...
                type=1
                b={
                abc= 123
                def = 456
                ghi=789
                }
                }
                
                object={
                ...
                type=2
                b={
                **aaa**= 987
                bbb = 654
                ccc=321
                }
                }
                
                object={
                ...
                type=3
                b={
                abc= 000
                fed = 001
                ihg=002
                }
                }
                
                object={
                ...
                type=2
                b={
                **aaa**= 987
                bbb = 654
                ccc=321
                }
                }
                
                1 Reply Last reply Reply Quote 0
                • guy038G Offline
                  guy038
                  last edited by guy038

                  Hi, @wagner-leung, @alan-kilborn and All,

                  My bad ! So, if I interpret your intended result, strictly, the regex S/R becomes :

                  SEARCH (?s)object((?!object).)+?type=2((?!object).)+?\K\babc(?=\h*=)

                  REPLACE **aaa**

                  Hope this version is the good one ! Of course, you may change the line type=2 and/or the variable abc as you like !

                  Cheers,

                  guy038

                  P.S. :

                  An other formulation of the search regex, using the free-spacing mode (?x) , would be :

                  SEARCH (?xs) object ((?!object).)+? type=2 ((?!object).)+? \K\b abc (?=\h*=)

                  For instance, the following search regex would search for the variable abc, in any object block containing, either, the line type=2 or type=3

                  SEARCH (?xs) object ((?!object).)+? type=[23] ((?!object).)+? \K\b abc (?=\h*=)

                  1 Reply Last reply Reply Quote 0

                  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