所以我有以下命令:
(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."
?
答案 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)。