What is the difference between [Regex]::Replace() and -replace?

房东的猫 提交于 2020-01-02 10:05:27

问题


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

回答1:


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.

    • Given that PowerShell is built on the .NET framework, it is a common pattern for PowerShell to surface .NET functionality in a simpler, higher-level fashion.
  • An important difference in default behavior is that -replace is case-INsensitive by default, in line with PowerShell's behavior in general.

    • Use variant -creplace for case-sensitive replacements.
  • -replace only provides a subset of the functionality provided by the various [Regex]::Replace() overloads.

    • The functionality gap has narrowed in PowerShell Core v6.1.0+, which now also offers callback functionality via a script block passed to -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.




回答2:


-Replace is a powershell operator that replaces X with Y and cannot be configured to do anything else.

[Regex] is a .NET class which contains a method called Replace and has many overloads that can configure and control how the string is replaced.

-replace probably uses [Regex]::Replace under the hood.

The reference to the Regex.Replace method contains all the many different ways it can be called.

The methods and properties contained in the Regex class.



来源:https://stackoverflow.com/questions/52332794/what-is-the-difference-between-regexreplace-and-replace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!