我有一個包含大量鏈接的 HTML 檔案。它們采用http:/oldsite/showFile.asp?doc=1234&lib=lib1
我想替換
的格式
http://newsite/?lib=lib1&doc=1234
(1234 和 lib1 是可變的)
關于如何做到這一點的任何想法?
謝謝P
uj5u.com熱心網友回復:
我不認為你的例子是正確的。
http:/oldsite/showFile.asp?doc=1234&lib=lib1 應該
http:/oldsite/showFile.asp?doc=1234&lib=lib1
和
http://newsite/?lib=lib1&doc=1234 應該 http://newsite?lib=lib1&doc=1234
要對這些進行替換,您可以執行
'http:/oldsite/showFile.asp?doc=1234&lib=lib1' -replace 'http:/oldsite/showFile\.asp\?(doc=\d )&(lib=\w )', 'http://newsite?$2&$1'
回傳 http://newsite?lib=lib1&doc=1234
要在檔案中替換這些,您可以使用:
(Get-Content -Path 'X:\TheHtmlFile.html' -Raw) -replace 'http:/oldsite/showFile\.asp\?(doc=\d )&(lib=\w )', 'http://newsite?$2&$1' |
Set-Content -Path 'X:\TheNewHtmlFile.html'
正則運算式詳細資訊:
http:/oldsite/showFile Match the characters “http:/oldsite/showFile” literally
\. Match the character “.” literally
asp Match the characters “asp” literally
\? Match the character “?” literally
( Match the regular expression below and capture its match into backreference number 1
doc= Match the characters “doc=” literally
\d Match a single digit 0..9
Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
& Match the character “&” literally
( Match the regular expression below and capture its match into backreference number 2
lib= Match the characters “lib=” literally
\w Match a single character that is a “word character” (letters, digits, etc.)
Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
uj5u.com熱心網友回復:
讀入檔案,遍歷每一行并用新值替換舊值,將輸出發送到新檔案:
gc file.html | % { $_.Replace('oldsite...','newsite...') } | out-file new-file.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/348784.html
標籤:电源外壳
