Reputation: 33
So I have the following command:
(Get-Content $path).Replace($current, $new) | Set-Content $path
It works fine for changing a string but lets say I have the following string in $path
: "This is a test. testing was done."
If i set $current
to "test"
and $new
to "Blah"
. I'll get "This is a Blah. Blahing was done."
How do I make it so that it only changes "test"
and not "testing"
so my string would be: "This is a blah. testing was done."
?
Upvotes: 3
Views: 2460
Reputation: 2596
$path = "This is a test. testing was done."
$current = "\btest\b"
$new = "Blah"
$path -replace ($current, $new)
\b is a regular expression word boundary
Upvotes: 3
Reputation: 10799
Instead of using the Replace()
method, try using the -replace
operator - this operator uses regular expressions as described in Get-Help about_Regular_Expressions (link is to MSDN).
Upvotes: 1