替换 PowerShell 中的子字符串 [英] Replace substring in PowerShell
本文介绍了替换 PowerShell 中的子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 -content-
形式的字符串,我想用 &content&
替换它.如何使用 PowerShell 中的替换来执行此操作?
I have a string in the form -content-
, and I would like to replace it with &content&
. How can I do this with replace in PowerShell?
推荐答案
内置的 -replace
运算符允许您为此使用正则表达式,例如:
The built-in -replace
operator allows you to use a regex for this e.g.:
C:\PS> '-content-' -replace '-([^-]+)-', '&$1&'
&content&
请注意,替换字符串中必须使用单引号,因此 PowerShell 不会解释 $1 捕获组.
Note the use of single quotes is essential on the replacement string so PowerShell doesn't interpret the $1 capture group.
这篇关于替换 PowerShell 中的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文