Community
    • Login

    Exit Python loop when replacement count is 0

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    7 Posts 2 Posters 898 Views 1 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.
    • M Andre Z EckenrodeM Offline
      M Andre Z Eckenrode
      last edited by

      When performing manual find/replace, the bottom of the dialog states how many occurrences were replaced. Is it possible to use some form of equivalent information in a Python script utilizing editor.rereplace in a loop, to exit when the last execution resulted in 0 replacements? If so, could someone provide an example? I’m not seeing any useful clues in the Python Script help.

      Example of code I want to have repeating in a loop:

      editor.rereplace(r'^(.+?) (\(tracks: .+?\)), (.+?) — (.+?)$', ur'\1 — \4 \2\r\n\3 — \4')
      
      Alan KilbornA 1 Reply Last reply Reply Quote 1
      • Alan KilbornA Offline
        Alan Kilborn @M Andre Z Eckenrode
        last edited by

        @M-Andre-Z-Eckenrode

        One technique you could use is to get the text of the document into a variable before the replacement (e.g. temp = editor.getText()), then get the text after the replacement, and see if it is the same as the saved variable, e.g. if editor.getText() == temp. If so, you know that the replacement replaced nothing.

        1 Reply Last reply Reply Quote 3
        • Alan KilbornA Offline
          Alan Kilborn
          last edited by

          @M-Andre-Z-Eckenrode

          This may also be of interest:

          Feature request: editor.rereplace() could return number of replacements made.

          1 Reply Last reply Reply Quote 3
          • Alan KilbornA Offline
            Alan Kilborn
            last edited by Alan Kilborn

            Maybe it goes without saying that you could get the match count before doing the replacement – and of course if the match count is zero then you can break out of your loop before doing a replacement that wouldn’t replace anything:

            while True:
                matches = []
                editor.research(your_regex, lambda m: matches.append(m.span(0)))
                if len(matches) == 0: break
                editor.rereplace(your_regex, ...)
            
            M Andre Z EckenrodeM 1 Reply Last reply Reply Quote 4
            • M Andre Z EckenrodeM Offline
              M Andre Z Eckenrode @Alan Kilborn
              last edited by

              @Alan-Kilborn

              All great ideas that I didn’t think of, and I like that last one the most, so I think I’ll go with it. Thanks much, Alan!

              Alan KilbornA 1 Reply Last reply Reply Quote 1
              • Alan KilbornA Offline
                Alan Kilborn @M Andre Z Eckenrode
                last edited by Alan Kilborn

                @M-Andre-Z-Eckenrode said in Exit Python loop when replacement count is 0:

                I like that last one the most

                A modification to that one could be to limit the matches that research() will look for to 1, e.g.:

                editor.research(your_regex, lambda m: matches.append(m.span(0)), 0, pos1, pos2, 1)

                The last argument of 1 will limit the possible matches to 1, which is sufficient to determine that there are matches remaining. This could speed up a slow running regex by not looking for things you don’t need.

                M Andre Z EckenrodeM 1 Reply Last reply Reply Quote 4
                • M Andre Z EckenrodeM Offline
                  M Andre Z Eckenrode @Alan Kilborn
                  last edited by

                  @Alan-Kilborn said in Exit Python loop when replacement count is 0:

                  A modification to that one could be to limit the matches that research() will look for to 1, e.g.:

                  Noted, and I like it. Thanks again.

                  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