Get-Content替换完整的字符串

时间:2017-06-15 13:54:28

标签: powershell

所以我有以下命令:

(Get-Content $path).Replace($current, $new) | Set-Content $path

它适用于更改字符串,但我可以说$path中有以下字符串:"This is a test. testing was done."

如果我将$current设置为"test"而将$new设置为"Blah"。我会得到"This is a Blah. Blahing was done."

如何制作它只会更改"test"而不是"testing"所以我的字符串会是:"This is a blah. testing was done."

2 个答案:

答案 0 :(得分:2)

 $path = "This is a test. testing was done."   
 $current = "\btest\b" 
 $new = "Blah"
 $path -replace ($current, $new)

\ b是正则表达式单词边界

答案 1 :(得分:1)

尝试使用Replace()运算符,而不是使用-replace方法 - 此运算符使用Get-Help about_Regular_Expressions中所述的正则表达式(链接到MSDN)。