Lar*_*tus 11 powershell replace
我在表单中有一个字符串-content-,我想用它替换它&content&.如何在PowerShell中替换?
Rog*_*mbe 15
PowerShell字符串只是.NET字符串,因此您可以:
PS> $x = '-foo-'
PS> $x.Replace('-', '&')
&foo&
Run Code Online (Sandbox Code Playgroud)
...要么:
PS> $x = '-foo-'
PS> $x.Replace('-foo-', '&bar&')
&bar&
Run Code Online (Sandbox Code Playgroud)
显然,如果要保留结果,请将其分配给另一个变量:
PS> $y = $x.Replace($search, $replace)
Run Code Online (Sandbox Code Playgroud)
Kei*_*ill 10
内置-replace运算符允许您使用正则表达式,例如:
C:\PS> '-content-' -replace '-([^-]+)-', '&$1&'
&content&
Run Code Online (Sandbox Code Playgroud)
请注意,在替换字符串中使用单引号是必不可少的,因此PowerShell不会解释$ 1捕获组.
| 归档时间: |
|
| 查看次数: |
20787 次 |
| 最近记录: |