Community
    • Login

    Replace string

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    14 Posts 5 Posters 710 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.
    • Alan KilbornA
      Alan Kilborn @astrosofista
      last edited by

      @astrosofista

      So this multiselect-via-Pythonscript-then-possibly-move-carets-via-BetterMulitiselection-then-insert-sequential-numbers-via-Column-Editor technique seems to be gaining some traction.

      One thing to be cautious of for the last step: When inserting numbers via Column Editor, if the number text inserted changes width anytime during the sequence, the field will be space-padded for numbers that are less than the maximum width. This may or may not cause a problem, depending upon one’s application.

      As an example, if you have 5 carets and you use the Column Editor to insert 8 through 12, what you’ll actually get is 8space, 9space, 10, 11, and 12. Obviously this doesn’t apply if you tick the Leading Zeroes checkbox–in that case you surely should know in advance what you’ll get–but it could be surprising and undesirable in some situations.

      astrosofistaA 1 Reply Last reply Reply Quote 2
      • Paolo SerpiP
        Paolo Serpi @astrosofista
        last edited by

        @astrosofista Yes! This is exactly what i’m lookin’ for! So i’ve installed Phyton plugin can you explain how you can do the multiselection?!

        Thanks

        Alan KilbornA astrosofistaA 2 Replies Last reply Reply Quote 0
        • Alan KilbornA
          Alan Kilborn @Paolo Serpi
          last edited by

          @Paolo-Serpi

          The Pythonscript shown here would need to be run after you’ve made a selection out of one section of your common text; like @astrosofista shows above with the TrackNode text. Reading other posts in the same thread will fill in any remaining gaps in the technique, I think.

          1 Reply Last reply Reply Quote 1
          • astrosofistaA
            astrosofista @Paolo Serpi
            last edited by

            @Paolo-Serpi said in Replace string:

            @astrosofista Yes! This is exactly what i’m lookin’ for! So i’ve installed Phyton plugin can you explain how you can do the multiselection?!

            Thanks

            Hi Paolo:

            Have you installed the Python script (PS) mentioned and linked by @Alan-Kilborn? If you did so, check Preferences > Editing > Multi-Editing Settings > Enable (Ctrl+Mouse click/selection) if needed. Then copy and paste the text to be changed in a new tab, so we are sure that replacements don’t apply in unwanted zones.

            1. Now, select the first TrackNode ( up to the number —1 in my gif animation—.
            2. Run PS in order to select all the instances to change.
            3. Press the Right Arrow only one time. All the carets are located at the left of the numbers to be replaced.
            4. Press Shift + End to select the numbers.
            5. Open Column Editor and enter the initial number of the series, as it is showed in my gif.

            Once you are done, press Escape get ride of all those carets.

            Paolo SerpiP 1 Reply Last reply Reply Quote 1
            • guy038G
              guy038
              last edited by guy038

              Hello, @paolo-serpi, @astro-sofista, @alan-kilborn and All,

              Here is an alternative solution to your problem, which uses regular expressions !

              Assuming this small input text, below :

              TrackNodes (2399
                  TrackNode ( 1
                      TrVectorNode (
                          blab blah
                          blab blah
                      )
                      TrPins ( 1 1
                          blah blah
                          blah blah
                      )
                  )
                  TrackNode ( 2
                      TrVectorNode (
                          blab blah
                          blab blah
                      )
                      TrPins ( 1 1
                          blah blah
                          blah blah
                      )
                  )
                  TrackNode ( 3
                      TrVectorNode (
                          blab blah
                          blab blah
                      )
                      TrPins ( 1 1
                          blah blah
                          blah blah
                      )
                  )
              
              • Put the caret at beginning of line TrackNodes (2399

              • Open the Replace dialog ( Ctrl + H )

              • SEARCH (?-is)(TrackNode\x20\(\x20)\d+|\R(?!.*?TrackNode)

              • REPLACE ?1\1><:|

              • Select the Regular expression search mode

              • Click on the Replace All button

              Immediately, we get the text :

              TrackNodes (2399
                  TrackNode ( ><|        TrVectorNode (|            blab blah|            blab blah|        )|        TrPins ( 1 1|            blah blah|            blah blah|        )|    )
                  TrackNode ( ><|        TrVectorNode (|            blab blah|            blab blah|        )|        TrPins ( 1 1|            blah blah|            blah blah|        )|    )
                  TrackNode ( ><|        TrVectorNode (|            blab blah|            blab blah|        )|        TrPins ( 1 1|            blah blah|            blah blah|        )|    )|
              

              Remark : I assume that the Vertical bar char ( | ), which replaces any line-break, does not exist, yet, in your file. If it’s not the case your may use, in the replace field, any other single standard char, not present in your file !

              Now, it’s almost obvious :

              • Put the caret on the first line TrackNode, between the two symbols > and <

              • Open the column editor ( Alt + C )

              • Select the choice Number to insert

              • Type in the number 8103 as initial number

              • Type in the value 1, as increment

              • Type in the value 1, as Repeat sequence

              • Do not tick the Leading zeros box

              • Click on the OK button

              • If necessary, delete the very last line, with the number only !

              You should get :

              TrackNodes (2399
                  TrackNode ( >8103<|        TrVectorNode (|            blab blah|            blab blah|        )|        TrPins ( 1 1|            blah blah|            blah blah|        )|    )
                  TrackNode ( >8104<|        TrVectorNode (|            blab blah|            blab blah|        )|        TrPins ( 1 1|            blah blah|            blah blah|        )|    )
                  TrackNode ( >8105<|        TrVectorNode (|            blab blah|            blab blah|        )|        TrPins ( 1 1|            blah blah|            blah blah|        )|    )|
              
              • Finally, perform the following regex S/R :

              SEARCH [<>]|(\|)

              REPLACE ?1\r\n ( or ?1\n if you work with UNIX files ! )

              And…, here is your expected text :

              TrackNodes (2399
                  TrackNode ( 8103
                      TrVectorNode (
                          blab blah
                          blab blah
                      )
                      TrPins ( 1 1
                          blah blah
                          blah blah
                      )
                  )
                  TrackNode ( 8104
                      TrVectorNode (
                          blab blah
                          blab blah
                      )
                      TrPins ( 1 1
                          blah blah
                          blah blah
                      )
                  )
                  TrackNode ( 8105
                      TrVectorNode (
                          blab blah
                          blab blah
                      )
                      TrPins ( 1 1
                          blah blah
                          blah blah
                      )
                  )
              

              If you like it, I’ll explain the regexes’s syntax, next time ;-))

              Best Regards,

              guy038

              1 Reply Last reply Reply Quote 2
              • astrosofistaA
                astrosofista @Alan Kilborn
                last edited by

                Hi @Alan-Kilborn

                In this particular case the BetterMultiSelection plugin is not needed, Just Notepad++ standard multiselection feature is the one that moves the carets. Know that because I disabled the plugin when checking the method.

                But yes, I also noticed those blank spaces and they could be problematic, as you said. If needed, a S/R regex can easily remove them. In Paolo’s case would be (?-s)TrackNode \( \d+\K\x20+, and leaving empty the Replace field.

                Best Regards.

                1 Reply Last reply Reply Quote 2
                • Paolo SerpiP
                  Paolo Serpi @astrosofista
                  last edited by

                  @astrosofista I installed the PS script following the discussion linked by @Alan-Kilborn, but i can’t run PS like you said at point 2.
                  Can you explain better how i can do it?

                  I try the method of @guy038 and works perfectly.

                  astrosofistaA 1 Reply Last reply Reply Quote 2
                  • astrosofistaA
                    astrosofista @Paolo Serpi
                    last edited by

                    @Paolo-Serpi said in Replace string:

                    i can’t run PS like you said at point 2.
                    Can you explain better how i can do it?

                    OK, once the Python script plugin has been installed and grabbed the Python code from the linked page:

                    1. Go to Plugins > Python Script > New Script.
                    2. Give it a name, say “Select all the same words”, and press save.
                    3. A new empty editor should appear. Copy the content into it and save it.

                    Please take care to paste the code as it is, avoiding any change, as Python is strict about whitespaces.

                    1. Go back to the TackNode page.
                    2. Select the first TrackNode ( , including the blank space before the number.
                    3. Run the Python script. To do so, go to Plugins > Python Script > Scripts, and select the PS as you named —“Select all the same words” in my example—.
                    4. All the TrackNode instances should be selected.
                    5. Continue with the other steps described in the previous post.

                    Let me know what happened.

                    Paolo SerpiP 1 Reply Last reply Reply Quote 3
                    • EkopalypseE
                      Ekopalypse
                      last edited by Ekopalypse

                      @Paolo-Serpi
                      In addition to what @astrosofista wrote,

                      • use the console (plugins->python script->show console) in case something doesn’t work as expected to see if an error is logged

                      • by using the plugins->python script->configuration dialog you can add
                        your script to menu items. Once this is done you can assign a shortcut
                        via run->modify shortcut/delete command dialog. Goto plugins tab,
                        search for the script and assign a shortcut.

                      1 Reply Last reply Reply Quote 3
                      • Paolo SerpiP
                        Paolo Serpi @astrosofista
                        last edited by

                        @astrosofista Well, i try a lot of times but never happen when i run PS following your suggestions. Notepad++ select only the first "tracknode ( ". I don’t know why but the only method that up to now work is the @guy038 ‘s method.
                        I hope that this method will be useful in similar situations, because i’m lookin’ for a standard method that i can always use in these cases.

                        Alan KilbornA astrosofistaA 2 Replies Last reply Reply Quote 0
                        • Alan KilbornA
                          Alan Kilborn @Paolo Serpi
                          last edited by

                          @Paolo-Serpi said:

                          select only the first "tracknode

                          Did you pay attention to what @astrosofista had said:

                          check Preferences > Editing > Multi-Editing Settings > Enable (Ctrl+Mouse click/selection)

                          You need to tick that Enable box.
                          This might be your problem.

                          Note: as an alternative, it could be scripted as follows:

                          editor.setMultipleSelection(True)  # in case not enabled in the Preferences
                          
                          1 Reply Last reply Reply Quote 3
                          • astrosofistaA
                            astrosofista @Paolo Serpi
                            last edited by

                            @Paolo-Serpi, @Alan-Kilborn

                            I see. Let’s make sure that the Python plugin is correctly installed. I suppose you installed it with the Plugins > Plugins Admin... menu.

                            Now we are going to see if it is working properly. Please click Plugins > Python Script > Show Console. On the bottom of the console there is a text box, please type in the following code

                            notepad.new()

                            and press Run. A new document should be opened. Got it?

                            If it worked, check again Preferences > Editing > Multi-Editing Settings > Enable (Ctrl+Mouse click/selection).

                            Try once more time to run the Python script, as described in previous posts.

                            Other than these ones, I am running out of ideas.

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