Community
    • Login

    Hi, looking for a regex that calculates distance between 2 numbers, then adjust the 2nd number to a minimum of 30.

    Scheduled Pinned Locked Moved General Discussion
    22 Posts 7 Posters 1.6k 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.
    • Jeff MichaelsJ
      Jeff Michaels @Mark Olson
      last edited by

      @Mark-Olson Hi Mark, I was able to get your regex code to properly select the number/number, but I’m unable to get the nppexec plugin to install in windows 11 arm. It doesn’t in the appear in the plugin list so I can select it to test the python portion after multiple installation attempts. Maybe pythonscript is not compatible with ARM.

      I also tried the code on my Mac with Bbedit and terminal, but every time I run the python code, it fails as it cannot go past editor.rereplace as displays a name error. Would you happen to know what I can replace it with to work directly in terminal or Bbedit? I’m unable to find a substitute. thank you

      Mark OlsonM 1 Reply Last reply Reply Quote 0
      • Mark OlsonM
        Mark Olson @Jeff Michaels
        last edited by Mark Olson

        @Jeff-Michaels
        You’re using an ARM64 architecture, not x64 or x86? Yeah, I do think that PythonScript only works on x64 or x86, but I wouldn’t know. Maybe somebody else can say. In any case NppExec and BbEdit won’t help you here - the Npp module exists only in PythonScript and it means absolutely nothing except when you’re running Notepad++ with PythonScript installed.

        In any case, if you can get PythonScript to work, you can download one of the .zip folders from here, unzip it, put it in the plugins folder of your Notepad++ installation, and then create a script in the scripts directory of the plugin folder containing the script I posted above.

        The ColumnsPlusPlus solution Coises described might make more sense if you have no intention of learning Python. PythonScript does have a non-negligible impact on the startup time of Notepad++, and while I love it because I’m familiar with Python, it’s harder to justify all this other overhead if you’re not.

        CoisesC 1 Reply Last reply Reply Quote 1
        • CoisesC
          Coises @Mark Olson
          last edited by

          @Mark-Olson said in Hi, looking for a regex that calculates distance between 2 numbers, then adjust the 2nd number to a minimum of 30.:

          The ColumnsPlusPlus solution Coises described

          I don’t have an ARM version of it, though, nor do I have any idea how I would test one, even if by some magical chance I could get it to build. :-( I didn’t even know there was an ARM version of Notepad++… or of “real” (non-RT) Windows, for that matter.

          Mark OlsonM 1 Reply Last reply Reply Quote 0
          • Mark OlsonM
            Mark Olson @Coises
            last edited by

            @Coises
            My vague (possibly mistaken) understanding is that Wine can be used to run Windows on ARM machines.

            Jeff MichaelsJ 1 Reply Last reply Reply Quote 0
            • Jeff MichaelsJ
              Jeff Michaels @Mark Olson
              last edited by Jeff Michaels

              @Mark-Olson Hi again Mark. I had forgotten that I have an Intel iMac with Windows 10 (in Parallels) still installed lying around here. I was able to install Python and Notepad++. Then I went to the Plugin admin and added the PythonScript plugin directly from there. Then pasted your code into a new Notepad window and saved it as a script so it was accessible from the Plugins/Python Script/Scripts menu. Then opened a new window in Notepad and pasted in one the files I want to fix. After running the script, nothing happened. What I really want to do is place a bunch of files (they have a .kbp extension, but are text files) in a folder on the Windows desktop and batch process them all in one shot with your script. Would you be able to help me do this? Thank you so much again.

              Jeff MichaelsJ 1 Reply Last reply Reply Quote 0
              • Jeff MichaelsJ
                Jeff Michaels @Jeff Michaels
                last edited by

                @Jeff-Michaels This is the error I’m getting at the moment

                File “C:\Program Files\Notepad++\plugins\PythonScript\scripts\kbpfix.py”, line 5
                return f’{n1}/{n2}’
                ^
                SyntaxError: invalid syntax

                Alan KilbornA 1 Reply Last reply Reply Quote 0
                • Alan KilbornA
                  Alan Kilborn @Jeff Michaels
                  last edited by Alan Kilborn

                  @Jeff-Michaels said in Hi, looking for a regex that calculates distance between 2 numbers, then adjust the 2nd number to a minimum of 30.:

                  This is the error I’m getting at the moment

                  Are you using a 2.x version of PythonScript?
                  If so, then it is probably caused by it not linking the f-string used in that line, as that is a Python3 feature.

                  In Python2 you could write that line as:

                  return '{n1}/{n2}'.format(n1=n1, n2=n2)

                  1 Reply Last reply Reply Quote 1
                  • Jeff MichaelsJ
                    Jeff Michaels
                    last edited by

                    (no success yet) I’m also trying to run the python code from Terminal on a Mac. Each time I run it, it comes up with a NameError. I placed the script in a folder on the desktop along with one of the text files I want to fix. As it cannot define the editor, the script will not run. Does anyone have any advice on how to get the script to run in Terminal with files in a folder on the desktop?

                    jeff@jeff ~ % python3 /Users/jeff/Desktop/test/kbpfix2.py
                    Traceback (most recent call last):
                    File “/Users/jeff/Desktop/test/kbpfix2.py”, line 14, in <module>
                    editor.rereplace(r’ (\d+)/(\d+)(?=/\d+[\r\n]±+$)', fix_distance)
                    NameError: name ‘editor’ is not defined

                    Mark OlsonM 1 Reply Last reply Reply Quote 0
                    • Jeff MichaelsJ
                      Jeff Michaels
                      last edited by Jeff Michaels

                      UPDATE: I am so close, but I still cannot get this to work. I’m trying to stick with Terminal on macOS, but it produces an error at ‘editor.rereplace’. Is there an alternative to this part of the code to get it to work on a Mac? (I usually use grep in Bbedit as my application)

                      I was able to point to a folder on my desktop that will process all the files in it with .kbp extensions. The Python script fails as it doesn’t know what ‘editor.rereplace’ is.

                      Here is my new terminal command to process the files in a folder (python script is located inside this folder as well):

                      for f in *.kbp; do
                        python3 kbpfix.py "$f" "${f%-fixed.kbp}"
                      done
                      

                      This is once again the original Python script that fails at the editor portion:

                      def on_match(m):
                          n1s, n2s = m.groups()
                          n1, n2 = int(n1s), int(n2s)
                          n2 = max(n1 + 30, n2)
                          return f'{n1}/{n2}'
                      
                      editor.rereplace(r'(?-i)(\d+)/(\d+)(?=/0\R\R-{29}\RPAGEV2)', on_match)
                      

                      this is the error that appears in the macOS terminal after running:

                          editor.rereplace(r'(?-i)(\d+)/(\d+)(?=/0\R\R-{29}\RPAGEV2)', on_match)
                      NameError: name 'editor' is not defined
                      

                      Is there a suggestion as to what I can replace for the editor to get it to work on macOS in terminal?

                      Alan KilbornA 1 Reply Last reply Reply Quote 0
                      • Alan KilbornA
                        Alan Kilborn @Jeff Michaels
                        last edited by

                        @Jeff-Michaels

                        I think you’re off-track.
                        This forum is about Notepad++ and its plugins.
                        We thought we were talking about PythonScript.
                        But apparently this has evolved into something that doesn’t relate to Notepad++, so we can’t continue to support you here.

                        If you want to continue on with Python, suggest you find a Python forum to keep going with your questions.

                        1 Reply Last reply Reply Quote 3
                        • Mark OlsonM
                          Mark Olson @Jeff Michaels
                          last edited by

                          @Jeff-Michaels

                          The script I wrote above can’t be run except through PythonScript in Notepad++. It does not work like a normal Python script. If you want to start a chat with me, I can help you write a normal, non-PythonScript-based Python script to solve your problem in a way that’s unrelated to Notepad++.

                          Jeff MichaelsJ 1 Reply Last reply Reply Quote 1
                          • Jeff MichaelsJ
                            Jeff Michaels @Mark Olson
                            last edited by Jeff Michaels

                            @Mark-Olson Hi again Mark. I was just successful at getting the script to run in Notepad++ and the PythonScript plugin (yeah). I had a folder of 77 files and I needed to open each file separately and run the script on each and save. It corrected my files perfectly. I have hundreds more to process though. Is there anyway to run the script on the entire folder at once?

                            Mark OlsonM 1 Reply Last reply Reply Quote 0
                            • Mark OlsonM
                              Mark Olson @Jeff Michaels
                              last edited by

                              @Jeff-Michaels
                              Glad you got it to work! Unfortunately, PythonScript isn’t very well-equipped to manipulate the file system outside of Notepad++. You could in principle write a solution that opened every file in the directory in Notepad++, applied my script above, closed the file, and moved on to the next. However, that would be very slow compared to just writing a normal Python script that doesn’t use Notepad++ at all.

                              Since my preferred solution to your larger problem doesn’t use Notepad++, I’ll turn to the chat.

                              Jeff MichaelsJ 1 Reply Last reply Reply Quote 0
                              • Jeff MichaelsJ
                                Jeff Michaels @Mark Olson
                                last edited by

                                @Mark-Olson Do you have a preferred way to chat or email? my email is jeff@sybersound.com

                                1 Reply Last reply Reply Quote 0
                                • Mark OlsonM
                                  Mark Olson
                                  last edited by

                                  Just want to close by making a general note for posterity.

                                  Out of curiosity, I tried using ChatGPT with Jeff’s problem, and it provided a solution that was suboptimal but serviceable nonetheless. And of course it was much much faster than me.

                                  So I guess what I’d say is this:
                                  If you’re looking for guidance on how to solve a problem that requires Notepad++, you’ve come to the right place. Not only that, I would very strenuously advise against trusting anything that ChatGPT says about Notepad++. However, if you don’t have any particular reason to believe that Notepad++ needs to be in the loop, ChatGPT might potentially be useful, as it excels at coming up with decent solutions to simple, routine problems, so long as you check its work.

                                  1 Reply Last reply Reply Quote 1
                                  • mkupperM
                                    mkupper
                                    last edited by

                                    To keep the thread on-topic, I see that the OP has things working within Notepad++ and apparently has fixed some of his files using Notepad++ with Notepad++'s flavor of PythonScript.

                                    The OP then asked if there is a way to do the script on all of the files in a folder. I believe it’s possible but don’t know the best practice for 1) How can someone open dozens, hundreds, or thousands of files in Notepad++? 2) How can someone then run a particular PythonScript from within Notepad++ on all open tabs?

                                    To address #1 something I do is to construct a Notepad++ session file that has all of the files that I want to work on and then run Notepad++.exe with -openSession filename. While that works for me I don’t know if that’s the best practice. It may well be the OP is satisfied with running Notepad.exe *.txt from the command line and letting Notepad++'s build in wild card expansion take over. I just tried it on a folder with 963 files and … a minute later had 963 new tabs open. I was surprised that “close all tabs to the right” took about 45 seconds but it worked.

                                    I don’t know how to address #2 - Is it possible to run a PythonScript on all open tabs?

                                    Alan KilbornA 1 Reply Last reply Reply Quote 2
                                    • Alan KilbornA
                                      Alan Kilborn @mkupper
                                      last edited by

                                      @mkupper said in Hi, looking for a regex that calculates distance between 2 numbers, then adjust the 2nd number to a minimum of 30.:

                                      Is it possible to run a PythonScript on all open tabs?

                                      No. What you would do is, in a single run of a single script, iterate over the open file tabs:

                                      for (filename, bufferID, index, view) in notepad.getFiles():
                                          notepad.activateFile(filename)
                                          ....
                                      

                                      After a file is “activated”, then the editor object can manipulate the data of the activated tab, e.g., perhaps do an editor.replace(), etc.

                                      1 Reply Last reply Reply Quote 5
                                      • Pan CardP
                                        Pan Card
                                        last edited by

                                        This post is deleted!
                                        Lycan ThropeL 1 Reply Last reply Reply Quote -3
                                        • Lycan ThropeL
                                          Lycan Thrope @Pan Card
                                          last edited by

                                          This post is deleted!
                                          1 Reply Last reply Reply Quote 0
                                          • First post
                                            Last post
                                          The Community of users of the Notepad++ text editor.
                                          Powered by NodeBB | Contributors