powershell - Powershell -replace 当替换字符串包含 $+
问题描述
我正在 PowerShell 中进行字符串替换。我无法控制要替换的字符串,但我可以通过这种方式重现我遇到的问题:
> 'word' -replace 'word','@#$+'
@#word
当我需要的实际输出是
> 'word' -replace 'word','@#$+'
@#$+
字符串$+
被扩展为被替换的单词,我无法找到阻止这种情况发生的方法。我尝试使用反引号(就像正常的 PowerShell 方式一样)转义$
with \
(好像它是一个正则表达式)。`
例如:
> 'word' -replace 'word',('@#$+' -replace '\$','`$')
@#`word
如何$+
在 PowerShell 中用文字替换?对于它的价值,我正在运行 PowerShell Core 6,但这也可以在 PowerShell 5 上重现。
解决方案
-replace
您可以使用如下.Replace()
方法,而不是使用运算符:
PS> 'word'.Replace('word','@#$+')
@#$+
该.Replace()
方法来自 .NET String 类,而-Replace
运算符是使用 .NET 实现的System.Text.RegularExpressions.Regex.Replace()
。
更多信息:https ://vexx32.github.io/2019/03/20/PowerShell-Replace-Operator/
推荐阅读
- python - 我怎么会单独得到这个文本?
- spring-boot - 使用 apache qpid 使用 amqpwss 连接到 azure 服务总线端口 443
- cython -
在赛通 - excel - 范围类的Excel vba 1004方法失败:自动填充到可变长度列
- stm32 - 如何在没有 vsnprintf 的情况下编写 UART 函数
- angular - Angular Jest 异步测试似乎将结果从一个测试溢出到另一个
- google-cloud-storage - GCP 存储桶转移
- delphi - Delphi - 多个 ADOQuery
- jquery - 如何在 JavaScript/jQuery 中为所选选项添加“必需”属性
- amazon-web-services - 定义 cloudFormation 脚本时,无法从 lambda 函数中的环境访问 SNS_TOPIC_ARN 值