Community

    • Login
    • Search
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    Exit Python loop when replacement count is 0

    Help wanted · · · – – – · · ·
    2
    7
    104
    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 Eckenrode
      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 Kilborn 1 Reply Last reply Reply Quote 1
      • Alan Kilborn
        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 Kilborn
          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 Kilborn
            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 Eckenrode 1 Reply Last reply Reply Quote 4
            • M Andre Z Eckenrode
              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 Kilborn 1 Reply Last reply Reply Quote 1
              • Alan Kilborn
                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 Eckenrode 1 Reply Last reply Reply Quote 4
                • M Andre Z Eckenrode
                  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
                  • First post
                    Last post
                  Copyright © 2014 NodeBB Forums | Contributors