I understood the difference between .Replace()
and -replace
, but what are -replace
and [Regex]::Replace()
?
I tested the 2 following codes, but for me the results are the exactly the same.
I also referred to PowerShell Cookbook(O'reilly), and it says
([Regex] is) extremely advanced regular expression replacement
I want to know what [Regex]
can but -replace
can't.
$line = "Loosen the socket by turning it#counterclockwise."
$line = $line -Replace "([a-z])#([a-z])","`$1 `$2"
$line
# Loosen the socket by turning it counterclockwise.
$line.GetType()
# IsPublic IsSerial Name BaseType
# -------- -------- ---- --------
# True True String System.Object
$line2 = "Loosen the socket by turning it#counterclockwise."
$line2 = [Regex]::Replace($line3,"([a-z])#([a-z])","`$1 `$2")
$line2
# Loosen the socket by turning it counterclockwise.
$line2.GetType()
# IsPublic IsSerial Name BaseType
# -------- -------- ---- --------
# True True String System.Object
The Fish's helpful answer contains good pointers, but let me frame things a little differently, in part inspired by Ansgar Wiechers' comments:
PowerShell's -replace
operator is a friendly wrapper for the .NET [Regex]::Replace()
method.
An important difference in default behavior is that -replace
is case-INsensitive by default, in line with PowerShell's behavior in general.
-creplace
for case-sensitive replacements.-replace
only provides a subset of the functionality provided by the various [Regex]::Replace()
overloads.
-replace
, thanks to work by Mathias R. Jessen; e.g.,'1 + 1 = 2' -replace '\d+', { [int] $_.Value * 2 }
yields '2 + 2 = 4'
and is the equivalent of:[regex]::replace('1 + 1 = 2', '\d+', { param($match) [int] $match.Value * 2 })
If -replace
is good enough for a given use case, use it rather than [regex]::Replace()
.
The syntax of method calls differs from the rest of PowerShell, and there are subtleties around type conversion and long-term stability of code; it is therefore generally preferable to stick with native PowerShell features (cmdlets and operators), if feasible.
However, if -replace
doesn't provide the functionality you need, calling [regex]::Replace()
directly is a great advanced option; note that Replace()
also exists as an instance method, in which case it offers additional functionality - e.g., the ability to limit the number of replacements; e.g.:
# Replace only the first two 'o' instances.
PS> $re = [regex] 'o'; $re.Replace('fooo', '@', 2)
f@@o
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With