• Login
Community
  • Login

Exit Python loop when replacement count is 0

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
7 Posts 2 Posters 436 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.
  • M
    M Andre Z Eckenrode
    last edited by Sep 16, 2022, 10:17 PM

    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')
    
    A 1 Reply Last reply Sep 16, 2022, 10:37 PM Reply Quote 1
    • A
      Alan Kilborn @M Andre Z Eckenrode
      last edited by Sep 16, 2022, 10:37 PM

      @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
      • A
        Alan Kilborn
        last edited by Sep 16, 2022, 11:56 PM

        @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
        • A
          Alan Kilborn
          last edited by Alan Kilborn Sep 17, 2022, 10:43 AM Sep 17, 2022, 10:42 AM

          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 1 Reply Last reply Sep 17, 2022, 1:50 PM Reply Quote 4
          • M
            M Andre Z Eckenrode @Alan Kilborn
            last edited by Sep 17, 2022, 1:50 PM

            @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!

            A 1 Reply Last reply Sep 17, 2022, 2:05 PM Reply Quote 1
            • A
              Alan Kilborn @M Andre Z Eckenrode
              last edited by Alan Kilborn Sep 17, 2022, 2:06 PM Sep 17, 2022, 2:05 PM

              @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 1 Reply Last reply Sep 17, 2022, 3:04 PM Reply Quote 4
              • M
                M Andre Z Eckenrode @Alan Kilborn
                last edited by Sep 17, 2022, 3:04 PM

                @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
                1 out of 7
                • First post
                  1/7
                  Last post
                The Community of users of the Notepad++ text editor.
                Powered by NodeBB | Contributors