简体   繁体   English

Notepad ++搜索路径引用并替换为文件的路径?

[英]Notepad++ search for path reference and replace with the file's path?

I am using notepad++ and I have a number of xml files. 我正在使用notepad ++,并且有许多xml文件。 For example, let's say the XML file is SomeXML.XML. 例如,假设XML文件是SomeXML.XML。

Within the file, there will be entries such as: //SERVER-NAME/Graphics/Materials/Downloaded/Fabric Grey.jpg 在文件中,将存在诸如以下内容的条目://服务器名称/图形/材料/已下载/ Fabric Grey.jpg

I wish to find these entries (they all start with \\SERVER-NAME and end with jpg or png) and replace them with: 我希望找到这些条目(它们都以\\ SERVER-NAME开头,以jpg或png结尾),并将它们替换为:

//The path to the SomeXML.XML/Fabric Grey.jpg //SomeXML.XML/Fabric Grey.jpg的路径

It must be doable - but I can't figure it out! 它必须可行-但我无法弄清楚! HELP. 救命。

-- Part 1: generic replacement -- -第1部分:通用替代品-

You could give the following regular expression a try: 您可以尝试以下正则表达式:

//.*/([^/]+\.(jpg|png))

To break it down: 分解:

  • 2 forward slashes 2个正斜杠
  • Followed by one or more characters ( . matches anything, + means 1 or more) 后跟一个或多个字符( .匹配任何字符, +表示1个或多个字符)
  • Followed by a slash 后跟一个斜线
  • Followed by anything that is not a slash: the filename before the extension ( [^\\] means anything except of slash, + once again means one or more). 后面跟非斜杠:扩展名前的文件名( [^\\]表示除斜杠之外的任何东西, +表示一个或多个)。
  • Followed by a dot ( \\. escapes the dot, so that is interpreted literally) 后面跟一个点( \\.转义点,因此按字面解释)
  • Followed by jpg or png ( | means OR) 后跟jpg或png( |表示OR)

Then just replace with whatever you like. 然后随便替换为您喜欢的任何东西。 If you use $1 in the replacement it will be replaced with the filename. 如果在替换中使用$ 1,它将替换为文件名。 So in your example SomeXML.XML/$1 would be replaced with SomeXML.XML/Fabric Grey.jpg . 因此,在您的示例中, SomeXML.XML/$1将替换为SomeXML.XML/Fabric Grey.jpg

notepad ++中“替换”窗口的屏幕截图

-- Part 2: replacing SomeXML.XML with the current filename -- -第2部分:用当前文件名替换SomeXML.XML-

Unfortunately putting in the filename can not be done in the same replace action. 不幸的是,不能在相同的替换操作中完成文件名的输入。 It has to be done for each file separately, but a macro can help speed it up. 必须分别为每个文件完成此操作,但是宏可以帮助加快速度。 Note that the steps below include recording said macro, so it is vital they are executed exactly as described. 请注意,以下步骤包括记录所述宏,因此至关重要的是,它们必须完全按所述执行。

  1. Open the files in notepad++ (having executed the above replacements first). 在notepad ++中打开文件(首先执行上述替换操作)。
  2. Click Macro -> Start Recording. 单击宏->开始录制。
  3. Press ctrl+f to open the find window. 按ctrl + f打开查找窗口。
  4. Go the the replace-tab. 转到替换标签。
  5. In the find-box, but SomeXML.XML . 在查找框中,但是SomeXML.XML
  6. Empty the replace with box. 清空替换为框。
  7. Click Edit -> Copy to Clipboard -> Current Filename to Clipboard. 单击编辑->复制到剪贴板->当前文件名到剪贴板。
  8. Press ctrl+v in the replace with box to paste the filename. 在替换为框中按ctrl + v粘贴文件名。
  9. Click Replace All (NOT in all opened documents). 单击全部替换(不在所有打开的文档中)。
  10. Close the replace-window. 关闭更换窗口。
  11. Click Macro -> Stop Recording. 单击宏->停止记录。
  12. Now in every file where you want to do the replacements, press Ctrl+Shift+P to execute the recorded macro. 现在在要替换的每个文件中,按Ctrl + Shift + P执行记录的宏。

Not fully automated, but this should already make your life quite a bit easier. 并非完全自动化,但这应该已经使您的生活变得更加轻松。

Here's a python script version: 这是python脚本版本:

import os.path
def replace_func(m):
try:
    head, tail = os.path.split(m.group(0))
    tail = "\\" + tail
    newname = os.path.dirname(notepad.getCurrentFilename()) + tail
    newname = newname.replace("\\","/")
    return newname
except:
    return m.group(0)

editor.rereplace('//.*/([^/]+\.(jpg|png))', replace_func)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM