[英]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: 分解:
.
matches anything, +
means 1 or more) 后跟一个或多个字符( .
匹配任何字符, +
表示1个或多个字符) [^\\]
means anything except of slash, +
once again means one or more). 后面跟非斜杠:扩展名前的文件名( [^\\]
表示除斜杠之外的任何东西, +
表示一个或多个)。 \\.
escapes the dot, so that is interpreted literally) 后面跟一个点( \\.
转义点,因此按字面解释) |
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
。
-- 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. 请注意,以下步骤包括记录所述宏,因此至关重要的是,它们必须完全按所述执行。
SomeXML.XML
. 在查找框中,但是SomeXML.XML
。 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.