2017-01-30 43 views
2

我有HTML內容的字符串。PowerShell代碼中查找和替換字符串

我想找到幾個字符,與其他一些字符串替換。

對於如:

$Content = "This is my string. For more information, goto http://www.example.com/searchpage.htm. You will get the required information here. You can also get information at http://www.example.com/searchpage1.htm. Do not replace sample.htm. Do not replace sample1.htm." 

無論我發現文本example.com,我需要example1.com更換和相同的URL包含.htm,應使.html

如果URL中不包含的例子。 COM,我不想取代的.htm 下面的代碼,我可以實現更換example.com 某些部分,但我不能在同一個UR,以取代部分名爲.htm L.

我不想更換的.htm各地的主要內容。 我只是想取代.htm無論我更換example.com

$find = "http://www.example.com/" 

$newContent = "http://www.example1.com/" 

$replacedContent = $Content -replace $strFind,$strReplace 

我想知道如何更換文本的多個部分中的長字符串。

請建議!

+0

'-replace '\ bexample \ .COM \ B','例1。 com'' –

回答

1

可能不是最安全的方式(如果網址不包含的.htm),但能爲你做到這一點:

$Content -replace 'http://www\.example\.com(.+?)\.html?', 'http://www.example1.com$1.html' 
+0

謝謝,但URL包含.htm。我需要修改包含example以及htm的URL到example1和.htm – user2598808

+0

您的意思是.html。我的片段完全是這樣 - 只是嘗試。 –

+0

不幸的是,這不適合我。我不知道我哪裏出錯了。請指引我 – user2598808