• Login
Community
  • Login

pythonscript: any ready pyscript to replace one huge set of regex/ phrases with others?

Scheduled Pinned Locked Moved Notepad++ & Plugin Development
58 Posts 10 Posters 19.9k 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.
  • V
    V S Rawat
    last edited by V S Rawat Jan 13, 2019, 7:35 PM Jan 13, 2019, 7:34 PM

    I use pythonscript, but I am not much conversant with its coding so I could do just simple macro and not make complex script so far.

    I have about 10 tables in which one set of regex/ phrases are to be replaced with other regex/ phrase, given correspondingly in the table itself.

    frankly, I am using foxreplace in firefox to do so, and it is working.

    tables are huge, some have 1000s of such phrases, and those 4-5 tables are to be replaced one after another on the same data.

    And the content of those tables change almost daily.

    I don’t think it will be possible to create py script for all of those regex/ phrases. That is not the way of programming, as I see it, those phrases are data, and there should be a script that picks data from those tables and does the replacement.

    Is there any such ready script that will read data from csv txt file and does the replacement, also include other option for replacement like fullwordmatch and casematch.

    Actually, I am looking for a script that does what foxreplace does.

    Please guide.

    Thanks a gig.

    A 2 Replies Last reply Jan 14, 2019, 1:41 PM Reply Quote 0
    • A
      Alan Kilborn @V S Rawat
      last edited by Jan 14, 2019, 1:41 PM

      @V-S-Rawat said:

      I am looking for a script that does what foxreplace does.

      Since you have a solution that works, why go looking for another one? Does having N++ do this for you give you some advantage?

      1 Reply Last reply Reply Quote 1
      • A
        Alan Kilborn @V S Rawat
        last edited by Jan 14, 2019, 2:05 PM

        @V-S-Rawat

        So a script to do something like you want is not really hard, something like this maybe:

        # format for each line is: delimiter then search regex then delimiter then replace regex
        sr_list = [
            '!a!A',
            '@b@B',
            '!c!C',
            ]
        
        # or take input from a file:
        #with open(r'sr_list.txt') as f: sr_list = f.readlines()
        
        editor.beginUndoAction()
        
        for line in sr_list:
            (s,r) = line[1:].rstrip().split(line[0])
            editor.rereplace(s,r)
        
        editor.endUndoAction()
        

        Of course it would have to be adapted to the exact format of your regex “data” if you don’t like the format that is in the script now.

        M 1 Reply Last reply Jan 14, 2019, 3:01 PM Reply Quote 2
        • M
          Meta Chuh moderator @Alan Kilborn
          last edited by Jan 14, 2019, 3:01 PM

          @Alan-Kilborn
          just tested and it works like magic. 👍

          little question about the python script plugin:
          do you, by any chance, know of any hidden button or hotkey, to directly open the path to the user scripts at …\plugins\Config\PythonScript\scripts from within the plugin’s menu items, instead of browsing there manually ?

          A 1 Reply Last reply Jan 14, 2019, 3:33 PM Reply Quote 1
          • A
            Alan Kilborn @Meta Chuh
            last edited by Jan 14, 2019, 3:33 PM

            @Meta-Chuh said:

            do you, by any chance, know of any hidden button or hotkey, to directly open the path to the user scripts at …, instead of browsing there manually ?

            Hmmm, not really…I guess if I wanted that I would make it a Favorite in the Explorer plugin. :)

            1 Reply Last reply Reply Quote 2
            • P
              PeterJones
              last edited by PeterJones Jan 14, 2019, 4:16 PM Jan 14, 2019, 4:13 PM

              @Meta-Chuh ,

              As I learned a few months ago (it’s buried in the docs, but I needed other users to point it out to me): to edit an existing script, instead of clicking normally on the script, use Ctrl+Click. After that, you can use the tabbar > right-click entry to open that directory in Explorer, if you find that easier for opening other scripts from the same folder.

              update: https://github.com/bruderstein/PythonScript/issues/108 was where I learned this.

              A M 2 Replies Last reply Jan 14, 2019, 4:41 PM Reply Quote 4
              • V
                V S Rawat
                last edited by Jan 14, 2019, 4:25 PM

                Since you have a solution that works, why go looking for another one?
                My data is in npp. I have to copy-paste it to firefox and run foxscript, and bring the processed data back to npp.

                Thanks.

                1 Reply Last reply Reply Quote 0
                • V
                  V S Rawat
                  last edited by Jan 14, 2019, 4:31 PM

                  @PeterJones

                  wow. so simple. thanks.

                  in my case, ctrl-leftclick on the script name in the menu, opens that script in npp to edit. rightclick doesn’t do anything.

                  1 Reply Last reply Reply Quote 0
                  • A
                    Alan Kilborn @PeterJones
                    last edited by Jan 14, 2019, 4:41 PM

                    @PeterJones

                    Yes, but seems like @Meta-Chuh was looking for something more direct.

                    1 Reply Last reply Reply Quote 1
                    • M
                      Meta Chuh moderator @PeterJones
                      last edited by Jan 14, 2019, 5:54 PM

                      @PeterJones said:

                      use Ctrl+Click

                      thats even better, thanks.
                      editing/viewing existing scripts is the thing i’ll probably use most in the future i suppose.

                      btw: i just read your old thread and i just had to copy the following sentence and apply it unchanged to myself ;-)

                      Well, I (me, metachuh) feel silly. It’s documented right there in the Installation: Usage in the help file: “To edit the script, just hold Ctrl down and click the script in the Scripts menu”. I guess that proves I’ve only read the help file when looking for specific commands. Someday, I should really read the whole thing. :-)

                      1 Reply Last reply Reply Quote 3
                      • G
                        guy038
                        last edited by guy038 Jan 15, 2019, 7:49 PM Jan 15, 2019, 7:47 PM

                        Hi, @alan-kilborn,

                        I cannot get your Python script ( see below ) running correctly ! I certainly misunderstood what it is for :-(( To my mind, assuming your list, it should replace any lower-case letter a, b and c with the corresponding upper-case letter, in current file, shouldn’t it ?

                        # format for each line is: delimiter then search regex then delimiter then replace regex
                        sr_list = [
                            '!a!A',
                            '@b@B',
                            '!c!C',
                            ]
                        
                        # or take input from a file:
                        #with open(r'sr_list.txt') as f: sr_list = f.readlines()
                        
                        editor.beginUndoAction()
                        
                        for line in sr_list:
                            (s,r) = line[1:].rstrip().split(line[0])
                            editor.rereplace(s,r)
                        
                        editor.endUndoAction()
                        

                        I tested your script, with Python script v1.2.0.0 and Notepad++ v6.0 and here is my debug info :

                        Notepad++ v7.6   (32-bit)
                        Build time : Nov 12 2018 - 23:51:42
                        Path : D:\@@\760\notepad++.exe
                        Admin mode : OFF
                        Local Conf mode : ON
                        OS : Windows XP (32-bit)
                        Plugins : BetterMultiSelection.dll ComparePlugin.dll CustomizeToolbar.dll DSpellCheck.dll LightExplorer.dll LuaScript.dll mimeTools.dll NppConverter.dll NppExec.dll NppExport.dll NppTextFX.dll PythonScript.dll 
                        

                        On the other hand, my Python installation seems correct as, for instance, your other script, below, works nice, highlighting the strings Hello there, in light blue Mark style 1

                        https://notepad-plus-plus.org/community/topic/16897/style-token-1-5-search-does-not-follow-default-case-sensitivity-of-find-dialog/5


                        So, Alan, what obvious thing am I missing ?!

                        Best Regards

                        guy038

                        A M 2 Replies Last reply Jan 15, 2019, 8:08 PM Reply Quote 3
                        • A
                          Alan Kilborn @guy038
                          last edited by Jan 15, 2019, 8:08 PM

                          @guy038

                          I am sorry to hear it didn’t work for you. :-(

                          It is really a simple script, I’m not sure what could go wrong. Maybe try putting this line before the rereplace line and perhaps it will provide a hint of some sort: console.write('s:{} r:{}\n'.format(s,r))

                          Also, but this would have nothing to do with it, the strings in sr_list should be raw strings for best use with regex without getting “LTS”. Thus, '!a!A' should be 'r!a!A' and same for any other strings put into the sr_list.

                          M 1 Reply Last reply Jan 15, 2019, 8:47 PM Reply Quote 3
                          • M
                            Meta Chuh moderator @guy038
                            last edited by Meta Chuh Jan 15, 2019, 9:05 PM Jan 15, 2019, 8:24 PM

                            @guy038

                            what happens if you try a complete new pythonscript 1.3.0.0 install on a new 7.6.2 portable ?
                            still the same, or does alan’s script work on that ?

                            i’ve added the folder locations, from the other py thread, below as convenience if lib location or anything else of the add ons might be a trigger:

                            get a new copy of PythonScript_Full_1.3.0.0.zip from here
                            extract it and put it’s contents as listed below

                            PythonScript.dll, plugin dll goes to:
                            npp.7.6.2.bin\plugins\PythonScript\PythonScript.dll

                            python27.dll goes to:
                            npp.7.6.2.bin\python27.dll

                            machine level scripts and python library go to:
                            npp.7.6.2.bin\plugins\PythonScript\lib\
                            npp.7.6.2.bin\plugins\PythonScript\scripts\
                               contains sample scripts and startup. py

                            manual, context-help files go to:
                            npp.7.6.2.bin\plugins\doc\PythonScript\
                               contains PythonScript.chm up to version 1.2.0.0
                               contains html docs since version 1.3.0.0

                            user level scripts go to:
                            npp.7.6.2.bin\plugins\config\PythonScript\scripts\
                               note: this folder will be created automatically as soon as a new script is created.

                            1 Reply Last reply Reply Quote 2
                            • M
                              Meta Chuh moderator @Alan Kilborn
                              last edited by Meta Chuh Jan 15, 2019, 8:48 PM Jan 15, 2019, 8:47 PM

                              @Alan-Kilborn said:

                              It is really a simple script

                              it may be simple, but it definitively comes in very handy.
                              i use it, love it, and without you i wouldn’t have it. 😃👍

                              before using your script, i used your suggested way of using macros for multi pass, multi regexes on files.
                              (and before that, there was pure darkness 😂 )

                              but now i prefer your script, as the regexes are much easier to change or read than within a saved macro. 👍👍👍

                              1 Reply Last reply Reply Quote 3
                              • G
                                guy038
                                last edited by guy038 Jan 16, 2019, 12:04 AM Jan 15, 2019, 11:53 PM

                                Hello @alan-kilborn, @meta-chuh and All,

                                Sorry, I preferred to take some time, doing numerous tests and … it works nicely ;-))

                                Alan, it’s just my mistake, because I should have opened the console, immediately ! Indeed, I wrote some accentuated characters above \x7f, although in a comment ! So I added the directive #coding=utf-8 as first line of the script. You’re really lucky as an [A-Z] person ;-))

                                In order to run S/R in an insensitive way, I just imported the re library and used the flag re.IGNORECASE

                                I also tried your second solution with strings/regexes in a file and… no problem, too !

                                Just notice that, in this case, the exact python code is, rather :

                                f = open(r'<Drive_Letter>:\....\....\sr_list.txt')
                                
                                sr_list = f.readlines()
                                

                                I, first thought that, according your comments, the part, to be inserted, was, literally :

                                open(r'sr_list.txt') as f: sr_list = f.readlines()
                                

                                But, of course, this code is wrong and an error occurs on word as -:))


                                Hi, @meta-chuh,

                                Hummmm…, I’m hungry, since a while… So be patient a bit ! I be back very soon and, as I’ve already installed a portable v7.6.2 version of N++, I’ll, simply, need to add the latest Python

                                Just before posting, I’ve seen your last reply to Alan and I do agree to your compliments ! It’s really a magic script ;-))

                                Best Regards,

                                guy038

                                1 Reply Last reply Reply Quote 4
                                • P
                                  PeterJones
                                  last edited by Jan 16, 2019, 12:32 AM

                                  An aside on as:

                                  @guy038 said:

                                  I, first thought that, according your comments, the part, to be inserted, was, literally :
                                  open(r'sr_list.txt') as f: sr_list = f.readlines()

                                  @Alan-Kilborn said:

                                  with open(r’sr_list.txt’) as f: sr_list = f.readlines()

                                  Having just done some Python tutorials, I learned about the with statement , which is what enables the as – the with was literally part of what was needed in order for the as to work

                                  1 Reply Last reply Reply Quote 2
                                  • A
                                    Alan Kilborn
                                    last edited by Alan Kilborn Jan 16, 2019, 2:05 AM Jan 16, 2019, 2:03 AM

                                    @guy038 said:

                                    It’s really a magic script

                                    It’s just a tiny, obvious script, at least to me. :)

                                    BTW glad you got it going.

                                    I think Peter straightened out the with/as stuff. I’ll just say that the line that started out #with was correct as written. The intent was that one would simply remove the comment (with ONE keypress) to enable the line. Note that my coding style is that if a line is an informational comment it starts out as # plus space plus text. If it is code that is commented out, it is # (at correct indent level), then no space(!), then code.

                                    [A-Z] person

                                    What is that? Why is it a lucky thing?

                                    M 1 Reply Last reply Jan 16, 2019, 9:14 AM Reply Quote 3
                                    • M
                                      Meta Chuh moderator @Alan Kilborn
                                      last edited by Meta Chuh Jan 16, 2019, 9:19 AM Jan 16, 2019, 9:14 AM

                                      @Alan-Kilborn

                                      Note that my coding style is that if a line is an informational comment it starts out as # plus space plus text. If it is code that is commented out, it is # (at correct indent level), then no space(!), then code

                                      very nice and clean to read 👍

                                      [A-Z] person

                                      hahaha, i never thought that [A-Z] person could be interpreted as a potential insult, or discrimination, but it is not. 😂😂😂

                                      it’s just an expression for languages without special characters and letters like áàñøö and so on.

                                      1 Reply Last reply Reply Quote 2
                                      • G
                                        guy038
                                        last edited by guy038 Jan 18, 2019, 7:16 PM Jan 17, 2019, 1:46 PM

                                        Hello @alan-kilborn, @meta-chuh and All,

                                        First, Alan, the expression “An [A-Z] person” is a common way, for @scott-sumner to point out that he’s poorly concerned about accentuated characters and all relative questions ! That’s why I said that you’re lucky for not having to bother about these problems ;-))

                                        Not also that I said, above, “is a common way” and not “was a common way” as I do hope that Scott will be back, on our forum, very soon !


                                        Now, of course, the Python syntax, below, is totally exact !

                                         with open(r'sr_list.txt') as f: sr_list = f.readlines()
                                        

                                        It’s just that when I saw the two comment lines :

                                        # or take input from a file:
                                        #with open(r'sr_list.txt') as f: sr_list = f.readlines()
                                        

                                        I thought, wrongly, it meant, in fact :

                                        # or take input from a file with [ the sentence ]:
                                        # open(r'sr_list.txt') as f: sr_list = f.readlines()
                                        

                                        BTW, Alan, I tested, in the sr_list.txt, the syntax |^|Test , with some space chars after the word Test and, unfortunately, the ending spaces are not taken in account. Of course, I could have used |^|Test\x20\x20\x20…

                                        So, may I ask for two improvements :

                                        • The possibility to repeat the separator, after the replacement string, to take extra blank chars in account, either, in the sr_list.txt file or in the script, itself

                                        • The possibility to add comments, beginning with the usual # char, in the sr-list.txt file

                                        For instance :

                                        # Add the string ABC, followed with 3 SPACES, at BEGINNING of line 
                                        |^|ABC   |
                                        # Add the string XYZ, followed with 3 SPACES  at END of line
                                        !$!XYZ   !
                                        

                                        Contrary to what I said, @meta-chuh, I didn’t come back and just preferred going to bed as I’ve planned to spend a ski-day, as weather was quite nice, Wednesday, on Grenoble and, in addition, I also met some friends of mine, in Chamrousse ski-station ;-))

                                        As promised, I installed the last 1.3.0.0. Python script version in my local N++ v7.6.2 installation

                                        Let’s suppose that is N++ v7.6.2 is installed in any folder XXXX, different from folder C\Program files and folder C\Program files (x86). Then,

                                        • I downloaded the PythonScript_Full_1.3.0.0.zip archive, in XXXX folder

                                        • With 7zFileManager, I extracted all archive’s contents, in the XXXX folder

                                        • I needed to execute an extra task :

                                          • Move the library PythonScript.dll from the plugins folder to the plugins\PythonScript folder
                                        • I opened Notepad v7.6.2

                                          • I chose the menu option Plugins > Python Script > New Script and, immediately closed the window, with the ESC key, in order to create the file tree XXXX > plugins > Config > PythonScript > scripts

                                        Finally, here is, below, the main file’s layout, right after installing the last Python script v1.3.0.0 ::

                                        XXXX, below, represents the INSTALL folder of N++ v7.6.2 , which must be DIFFERENT from, either, "C\Program files" and "C\Program files (x86)"
                                        
                                        It's IMPORTANT to note that this LOCAL installation needs the ZERO-LENGTH file, "doLocalConf.xml", along with "notepad..exe"
                                        
                                        XXXX
                                            \
                                            |-- autoCompletion (folder)
                                            |                \
                                            |                |-- ".xml" files
                                            |
                                            |-- localization (folder)
                                            |              \
                                            |              |-- ".xml" files
                                            |
                                            |-- plugins (folder)
                                            |         \
                                            |         |-- Config (folder)
                                            |         |        \
                                            |         |        |-- Hunspell (folder)
                                            |         |        |          \
                                            |         |        |          |-- en_US.aff
                                            |         |        |          |
                                            |         |        |          |-- en_US.dic
                                            |         |        |
                                            |         |        |-- PythonScript (folder)
                                            |         |        |              \
                                            |         |        |              |-- scripts (folder)
                                            |         |        |                        \
                                            |         |        |                        |-- Future USER ".py" scripts
                                            |         |        |
                                            |         |        |-- ".ini" files
                                            |         |        |
                                            |         |        |-- nppPluginList.dll
                                            |         |
                                            |         |-- doc (folder)
                                            |         |     \
                                            |         |      |-- PythonScript(folder)
                                            |         |                     \
                                            |         |                     |-- _sources (folder)
                                            |         |                     |
                                            |         |                     |-- _static  (folder)
                                            |         |                     |
                                            |         |                     |-- ".html" files and Miscellaneous files
                                            |         |
                                            |         |-- DSpellCheck (folder)
                                            |         |             \
                                            |         |             |-- DSpellCheck.dll
                                            |         |
                                            |         |-- mineTools (folder)
                                            |         |           \
                                            |         |           |-- mineTools.dll
                                            |         |
                                            |         |-- NppConverter (folder)
                                            |         |              \
                                            |         |              |-- NppConverter.dll
                                            |         |
                                            |         |-- NppExport
                                            |         |           \
                                            |         |           |-- NppExport.dll
                                            |         |
                                            |         |-- PythonScript (folder)
                                            |         |              \
                                            |         |              |-- lib (folder)
                                            |         |              |     \
                                            |         |              |      |-- Sub-folders
                                            |         |              |      |
                                            |         |              |      |-- ".py" files
                                            |         |              |
                                            |         |              |-- scripts (folder)
                                            |         |              |         \
                                            |         |              |         |-- Samples (folder)
                                            |         |              |         |         \
                                            |         |              |         |         |-- ".py" scripts
                                            |         |              |         |
                                            |         |              |         |-- startup.py
                                            |         |              |
                                            |         |              |-- PythonScript.dll
                                            |
                                            |-- themes (folder)
                                            |        \
                                            |        |-- ".xml" files
                                            |
                                            |-- updater (folder)
                                            |         \
                                            |         |-- GUP.exe
                                            |         |
                                            |         |-- gup.xml
                                            |         |
                                            |         |-- libcurl.dll
                                            |
                                            |-- doLocalConf.xml
                                            |
                                            |-- Notepad++.exe
                                            |
                                            |-- python27.dll
                                            |
                                            |-- SciLexer.dll
                                            |
                                            |-- ".txt" files
                                            |
                                            |-- ".xml" CONFIGURATION files
                                        

                                        Best Regards,

                                        guy038

                                        P.S. :

                                        In the future, I think that, at least, for portable installs, when all problems concerning “Plugins Admin” are solved, it would be reasonable to migrate the Config and doc directories from the plugins folder to the higher level, with the other directories localization, autoCompletion, themes and updater

                                        So, the plugins folder would only contains sub-folders relative to each plugin installed ! What do you think of my idea ?

                                        1 Reply Last reply Reply Quote 3
                                        • G
                                          guy038
                                          last edited by guy038 Jan 17, 2019, 1:57 PM Jan 17, 2019, 1:56 PM

                                          Hello @alan-kilborn, @meta-chuh and All,

                                          I’m answering to myself, concerning the last question, at the end of my previous post

                                          Eventually, it would not be a nice solution to do so as, indeed, the Config and doc folders contain, both, files rather relative to plugins, too !

                                          Cheers,

                                          guy038

                                          Meta ChuhM 1 Reply Last reply Jan 17, 2019, 2:36 PM Reply Quote 3
                                          1 out of 58
                                          • First post
                                            1/58
                                            Last post
                                          The Community of users of the Notepad++ text editor.
                                          Powered by NodeBB | Contributors