If, as your question indicates, you want to use N ++, then use N ++ Python Script. Install a script and assign a key combination, then you have a one-pass solution requiring only opening, changing and saving ... it couldn't be much easier.
I think part of the problem is that N ++ is not a regular expression tool and using a special regular expression tool, or even a search / replace solution, is sometimes justified. You may be better, both in speed and in time, using a tool designed for word processing and editing.
[Script Change] :: Changed to match the expected changes / conclusions.
# Substitute & Replace within matched group. from Npp import * import re def repl(m): return "(Scripts." + re.sub( "[-.]", "_", m.group(1) ).replace( "/", "." ) + ")" editor.pyreplace( '(?:[(].*?Scripts.)(.*?)(?:"?[)])', repl )
- Install :: Plugins β Plugin Manager β Python script
- New Script :: Plugins β Python script β script -name.py
- Select the target tab.
- Run :: Plugins β Python script β Scripts β script -name
[Edit: extended single-line PythonScript command]
Need for a new regex module for Python (which I hope will replace re), I played and compiled it for use with the N ++ PythonScript plugin and decided to test it on your sample set.
Two commands on the console ended with the correct results in the editor.
import regex as re editor.setText( (re.compile( r'(?<=.*Content[(].*)((?<omit>["~]+?([~])[/]|["])|(?<toUnderscore>[-.]+)|(?<toDot>[/]+))+(?=.*[)]".*)' ) ).sub(lambda m: {'omit':'','toDot':'.','toUnderscore':'_'}[[ key for key, value in m.groupdict().items() if value != None ][0]], editor.getText() ) )
Very sweet!
What else sets us apart from using regex instead of re was that I was able to build an expression in Expresso and use it as is! This allows you to get a detailed explanation, just copy the fragment of the string r'' into Expresso.
Short text:
Match a prefix but exclude it from the capture. [.*Content[(].*] [1]: A numbered capture group. [(?<omit>["~]+?([~])[/]|["])|(?<toUnderscore>[-.]+)|(?<toDot>[/]+)], one or more repetitions Select from 3 alternatives [omit]: A named capture group. [["~]+?([~])[/]|["]] Select from 2 alternatives ["~]+?([~])[/] Any character in this class: ["] [toUnderscore]: A named capture group. [[-.]+] [toDot]: A named capture group. [[/]+] Match a suffix but exclude it from the capture. [.*[)]".*]
The damage to the command is pretty elegant, we say that Scintilla sets the full buffer contents for the results of the compiled regular expression substitution command, essentially using a "switch" on behalf of a group that is not empty.
Hopefully Dave (author of PythonScript) will add a regular expression module to the ExtraPythonLibs part of the project.