-replace 不会用“()"替换字符串; [英] -replace does not replace string with "()"

查看:40
本文介绍了-replace 不会用“()"替换字符串;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试替换 PowerShell 中包含括号的字符串.但是,当我尝试这样做时,它不起作用.

I'm trying to replace a string containing parentheses in PowerShell. However, when I try this to do this it is not working.

知道我哪里出错了吗?我应该怎么做才能在 PowerShell 中用 -replace 替换包含 () 的字符串?

Any idea as to where I'm going wrong? What am I suppose to do to replace a string containing () with -replace in PowerShell?

$a='Some Text with (round) brackets. This text is long.'
$ch="with (round) brackets."
$b="this text"
$a -replace $ch,$b

输出:

Some Text with (round) brackets. This text is long.

推荐答案

-replace 使用 正则表达式 所以你必须逃避你的正则表达式:

-replace useses regular expression so you have to to escape your regex:

$a='Some Text with (round) brackets. This text is long.'
$ch="with (round) brackets."
$b="this text"
$a -replace [regex]::Escape($ch),$b

输出:

Some Text this text This text is long.

这篇关于-replace 不会用“()"替换字符串;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆