2

I have about 200 regular expression searches and it's a pain to copy and paste them one by one. Is there an addon/plugin for Notepad++ that would automatically fill in the Search For and Replace fields or something of the sorts? If not is there a better way to do it? I would be dealing with the same file and using all of the same searches and probably add more searches as time goes on.

1 Answer 1

1

Notepad++ is not suited for this kind of batch work. I'd recommend you to find another tool, or try a scripting language.

If you really want to use Notepad++ for this task, I think you have only 2 options:

  • Macros. You can record a macro while you are applying your regular expressions one-by-one in the Replace dialog. Save it, and then you can Play the Macro whenever and wherever you want. Caveat: you can't easily add new regular expressions. Actually it's possible by modifying manually the shortcuts.xml that you will find in your %APPDATA%\Notepad++\ folder. This is where the recorded Macros are stored after you restart Notepad++. You can cut a new Macro with your new regular expressions and paste it in your older Macro. But it's a dirty job, I'll suggest you to move on to the next option.

  • Python Script plugin. You can write a Python script applying all your regular expressions. Once the script created, it will be very easy to add new regular expressions. Check a previous post of mine on how to install and use the plugin and then edit the following script (let's name it batch_regex.py) accordingly:

    editor.pyreplace(r"foo", r"bar") #this is a Python regular expression
    editor.pyreplace(r"baz", r"qux") #add here new regular expressions
    editor.pyreplace(r"test", r"yeah") #your third regular expression
    #etc...
    
    #if you have multiline regular expressions use pymlreplace instead (documentation: http://npppythonscript.sourceforge.net/docs/latest/scintilla.html#pymlreplace)
    
13
  • Thanks for responding. That sounds like what Im wanting to do. I will try it out but I know nothing of python but from what you describe I shouldnt have any problems. I assume the r"foo" (mine will be regexp) is what it will search for and r"bar" (mine will be regular text) is what it will replace it with. Is this correct? Commented Nov 20, 2013 at 19:37
  • Correct. Give it a try and if you have any trouble let me know.
    – psxls
    Commented Nov 20, 2013 at 19:39
  • I got it setup and so far it works GREAT. Dont know if Ill have any problems later but so far so good. I did notice that I dont need the r"bar" to insert regular text, instead deleted the r and have "bar", unless you see a problem there. Commented Nov 20, 2013 at 20:49
  • The r"" notation is not necessary, so proceed with no fear! But in some regular expressions it might be useful. Read more on regexes from Python's official documentation
    – psxls
    Commented Nov 20, 2013 at 22:06
  • This definately was the answer I was looking for and thanks again for the info. I thought there was something to click for "solved" and I just now found where to click. Commented Nov 21, 2013 at 1:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.