[英]Searching and replacing text in html file with notepad++ using regular expression
我們正在嘗試在使用 Notepad++ 打開的 html 文件中搜索和替換文本。
我們需要更新標簽“a href”和“img src”內的路徑,維護文件的名稱(文件中的文件名和路徑不同)
所以我們需要找到所有這些行:
<a href="File://///name.it.domain.com/PATH/file name.extension"
<img src="PATH/file name.extension"
並將 PATH/ 更改為 images/,對於任何 PATH,保持文件名:
<a href="images/file name.extension"
<img src="images/file name.extension"
這里有一些例子:
<a href="File://///name.it.domain.com/directory/name/this is a butterfly.pdf"
應該改為<a href="images/this is a butterfly.pdf"
和
<a href="C:/party/koala/main.doc"
應該改為<a href="images/main.doc"
<img src="it.free.main/doll/hello.jpg"
應該改為<img src="images/hello.jpg"
文件中的路徑具有共同的起始表達式,例如(File://///name.it.domain.com 或 C:/ 等)所以我正在嘗試使用表達式file:.{number}(.*)
在查找哪個字段並在替換字段$1/images
中它可以工作,但它不是最佳解決方案,因為我必須在每個文件中主要更改它,任何人都可以幫助我們找到更通用的解決方案?
實現此目的的一種方法是查找最后一次出現的反斜杠字符,因為您的示例表明可以依賴至少一次的存在。
find
正則表達式:
(href|src)=".*\\(.*)"
replace
正則表達式:
$1="images\\$2"
您可以在此處通過您提供的示例看到這一點。
(?:href|src)="\K[^"]+(?=/[^/."]+\.[^/."]+)
images
. matches newline
. matches newline
解釋:
(?: # non capture group
href # literally
| # OR
src # literally
) # end group
=" # literally
\K # forget all we have seen until this position
[^"]+ # 1 or more any character that is not a double quote
(?= # positive lookahead, make sure we have after:
/ # a slash
[^/."]+ # 1 or more any character that is not slash, dot or quote
\. # a dot
[^/."]+ # 1 or more any character that is not slash, dot or quote
) # end lookahead
截圖(之前):
截圖(之后):
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.