问题描述
我理解了 .Replace()
和 -replace
之间的区别,但是什么是 -replace
和 [Regex]::Replace()
?
I understood the difference between .Replace()
and -replace
, but what are -replace
and [Regex]::Replace()
?
我测试了以下 2 个代码,但对我来说结果完全相同.
I tested the 2 following codes, but for me the results are the exactly the same.
我还提到了 PowerShell Cookbook(O'reilly),它说
I also referred to PowerShell Cookbook(O'reilly), and it says
([Regex] 是) 极其先进的正则表达式替换
我想知道什么 [Regex]
可以但 -replace
不能.
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 的有用答案 包含很好的指针,但是让我稍微改变一下,部分灵感来自 Ansgar Wiechers 的评论:
The Fish's helpful answer contains good pointers, but let me frame things a little differently, in part inspired by Ansgar Wiechers' comments:
PowerShell 的
-replace
operator 是一个友好的包装器 用于 .NET[Regex]::Replace()
方法.
- 鉴于 PowerShell 基于 .NET 框架构建,因此 PowerShell 以更简单、更高级别的方式呈现 .NET 功能是一种常见模式.
默认行为的一个重要区别是 -replace
是 case-INsensitive 默认情况下,符合 PowerShell 在一般.
An important difference in default behavior is that -replace
is case-INsensitive by default, in line with PowerShell's behavior in general.
- 使用变体
-creplace
进行大小写敏感替换.
- Use variant
-creplace
for case-sensitive replacements.
-replace
仅提供各种 [Regex]::Replace()
重载.
-replace
only provides a subset of the functionality provided by the various [Regex]::Replace()
overloads.
- PowerShell Core v6.1.0+ 中的功能差距已经缩小,现在还通过传递给
-replace
回调功能>,感谢 Mathias R. Jessen 的工作;例如,'1 + 1 = 2' -replace '\d+', { [int] $_.Value * 2 }
产生'2 + 2 = 4'
并且是相当于:[regex]::replace('1 + 1 = 2', '\d+', { param($match) [int] $match.Value * 2 })
- 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 })
如果 -replace
对给定用例足够好,请使用 it 而不是 [regex]::Replace()
.
If -replace
is good enough for a given use case, use it rather than [regex]::Replace()
.
方法调用的语法与 PowerShell 的其余部分不同,并且在类型转换和代码的长期稳定性方面存在细微差别;因此,如果可行,通常最好坚持使用原生 PowerShell 功能(cmdlet 和运算符).
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.
但是,如果 -replace
没有提供您需要的功能,直接调用 [regex]::Replace()
是一个很好的高级选项;请注意,Replace()
也作为实例 方法存在,在这种情况下,它提供了附加功能 - 例如,限制替换次数例如:
# Replace only the first two 'o' instances.
PS> $re = [regex] 'o'; $re.Replace('fooo', '@', 2)
f@@o
这篇关于[Regex]::Replace() 和 -replace 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!