stefankueng / grepWin

A powerful and fast search tool using regular expressions
https://tools.stefankueng.com/grepWin.html
GNU General Public License v3.0
1.77k stars 190 forks source link

Replace all / Multiple replacement sequences at a time #92

Open me-suzy opened 5 years ago

me-suzy commented 5 years ago

hello. I use many regex formulas to search and replace many things. But there are some regex formulas that must be use not just for one single Replace, but more sequences of Replaces.

For example, in this regex I must find any "double space" from the tag <p class="text_obisnuit"> and to replace with one single tag. But to succeed this in grepWin, I must press Replace button for many times.

\s\S*(?:<p class="text_obisnuit">|\G)(?:(?!</p>).)*?\s\K\s+|(?<=<p class="text_obisnuit">)\s+|\s+(?=</p>)

So, basically, I need a "Replace all / Multiple replace sequence at once", so not to press Replace so many times.

4evermaat commented 5 years ago

I don't quite understand this one. Do you mean to perform multiple regex searches at once? That you are search/replace something. Then it creates a different set of text in which you would then search/replace again (with the same search string???)

I would think you group everything that you need to save, search for the phrases/regex with grouping, and then rearrange the groups in the replace.

Maybe I am misunderstanding something.

me-suzy commented 5 years ago

It is something like a checkbox, that will continue to loop the regex formula (search and replace), until I get the message "Found 0 matches in 0 Files" (or "All sequences done" ). Bacause there are many regex formula where I must press too many times the button "Replace".

another alternative, it was to make a Batch Editor, such as TextCrawler software, where I have the possibilities to load as many regex formula I want, and will continuous loop until is done. See this print screen: https://snag.gy/oiLVag.jpg

4evermaat commented 5 years ago

cant you just put all the conditions into 1 regex search? You can have 100 conditions if you really want in one huge regex.

the only other option is to break it up into several regex searches and run each replace command with CMD/CLI. I'm experimenting with a quick command line option now. I have some files that need to remove some characters . so the files are generated every day. And then I would have to run the search/replace immediately afterwards to remove the characters so that I can do other work with those files


edit: thank you for the textCrawler link

me-suzy commented 4 years ago

hello, what is CMD/CLI and how can I use it? Can i use it with GrepWin ?

4evermaat commented 4 years ago

@me-suzy CMD is the command prompt CLI is command line interface. Both phrases imply that you can operate certain functions by running arguments at the time the program is run.

In this way, you can setup a batch file, task scheduler, or other forms of automation.


I do think TextCrawler Pro does a much better job of stacking multiple regex searches in batch mode.

Also easier to test regex function.

me-suzy commented 4 years ago

indeed, TextCrawler does not crash on more then 500 files, and have the Batch Processor feature. But some operators does not work on TextCrawler, such as \K, because .net does not supported. For now...

4evermaat commented 4 years ago

indeed, TextCrawler does not crash on more then 500 files, and have the Batch Processor feature. But some operators does not work on TextCrawler, such as \K, because .net does not supported. For now...

\K is LookBehind function, correct? You must be doing some serious regex. But I suppose that is the power of such tools.

Check this thread

me-suzy commented 2 years ago

you can make a multiple replacement very simple. You can make the "Presents" button to load the same "bookmarks" file, but instead one single Find/Replace , you can make multiple F/R lines with Regex.

For example, when I load the Present named: "My First Multiple Replacements", basicaly I will load and run more FIND/REPLACE bookmark lines, even if in the graphical user interface will show one single regex.

"My First Multiple Replacements"

[REGEX 1]
backup=false
casesensitive=true
dotmatchesnewline=false
excludedirs=""
filematch="*.html|*.htm|*.txt"
filematchregex=false
includebinary=false
includefolder=false
includehidden=false
includesystem=false
replaceString="TEST-REPLACE"
searchpath=c:\python-test
searchString="TEST-FIND"
useregex=false
utf8=true
wholewords=false

[REGEX 2]
backup=false
casesensitive=true
dotmatchesnewline=false
excludedirs=""
filematch="*.html|*.htm|*.txt"
filematchregex=false
includebinary=false
includefolder=false
includehidden=false
includesystem=false
replaceString="TEST-REPLACE"
searchpath=c:\python-test
searchString="TEST-FIND"
useregex=false
utf8=true
wholewords=false

[REGEX 2]
backup=false
casesensitive=true
dotmatchesnewline=false
excludedirs=""
filematch="*.html|*.htm|*.txt"
filematchregex=false
includebinary=false
includefolder=false
includehidden=false
includesystem=false
replaceString="TEST-REPLACE"
searchpath=c:\python-test
searchString="TEST-FIND"
useregex=false
utf8=true
wholewords=false
agilis94 commented 2 years ago

Hello, I don't get it. Would you explain it again ? Is it possible to execute multiple F/R ?

me-suzy commented 2 years ago

Hello, I don't get it. Would you explain it again ? Is it possible to execute multiple F/R ?

yes, also see this code here (this is working in notepad++ only )

https://superuser.com/questions/1719553/how-to-make-a-pop-up-prompt-message-on-notepad-plugins-python-script?noredirect=1#comment2654338_1719553

4evermaat commented 2 years ago

Hello, I don't get it. Would you explain it again ? Is it possible to execute multiple F/R ?

yes, also see this code here (this is working in notepad++ only )

https://superuser.com/questions/1719553/how-to-make-a-pop-up-prompt-message-on-notepad-plugins-python-script?noredirect=1#comment2654338_1719553

can you please link to a new answer or type out the answer here? SuperUser seems to have deleted the answer.

me-suzy commented 2 years ago

You can use this code to make multiple Find and Replace on Notepad++

Maybe in the future this option (Multiple Find and Replace) will be available in grepwin as well. This king of Batch Processor is very important.

    # -*- coding: utf-8 -*-
    from __future__ import print_function

    from Npp import *
    import os
    import sys

    #-------------------------------------------------------------------------------

    class T22601(object):

        def __init__(self):
            top_level_dir = notepad.prompt('Paste path to top-level folder to process:', '', '')
            number_files = 0
            for root, dirs, files in os.walk(top_level_dir):
                nested_levels = root.split('/')
                if len(nested_levels) > 0:
                    del dirs[:]
                for filename in files:
                    if filename[-5:] == '.html':
                        notepad.open(root + "\\" + filename)
                        console.write(root + "\\" + filename + "\r\n")
                        notepad.runMenuCommand("Encodage", "Convertir en UTF-8")
                        regex_find_repl_dict_list = [
                            { 'find' : '<title>(.*?)</title>', 'replace' : r'\3\2\1' },
                            { 'find' : '<head>\s+', 'replace' : r'\n baba ' },
                        ]
                        editor.beginUndoAction()
                        for d in regex_find_repl_dict_list: editor.rereplace(d['find'], d['replace'])
                        editor.endUndoAction()
                        notepad.save()
                        notepad.close()
                        number_files += 1
            result = notepad.prompt('Number of modified files: ', '', number_files)
            print("Number of modified files: ", number_files)
    #-------------------------------------------------------------------------------

    if __name__ == '__main__': T22601()
garry-ut99 commented 1 year ago

TextCrawler : Link

By the way, also 3 more Find & Replace in Files tools which have batch replace feature :