powershell替换具有特殊字符的字符串不起作用

时间:2015-01-25 04:43:13

标签: powershell

$strText = '<httpHandlers>
      <remove verb="GET,HEAD,POST" path="*" />
    other content ....
        </httpHandlers>

$Pattern = '<remove verb="GET,HEAD,POST" path="*" />'
$New ='<remove verb="GET,HEAD,POST" path="*" NEW CONTENT ADDED />'
$strReplace = [regex]::replace($strText, $pattern, $New)`  

似乎不起作用

1 个答案:

答案 0 :(得分:1)

$strText = '<httpHandlers>
      <remove verb="GET,HEAD,POST" path="*" />
    other content ....
        </httpHandlers>'

$Pattern = [regex]::escape('<remove verb="GET,HEAD,POST" path="*" />')
$New ='<remove verb="GET,HEAD,POST" path="*" NEW CONTENT ADDED />'
$strReplace = [regex]::replace($strText, $pattern, $New)
$strReplace
  1. 您的字符串$strText缺少报价
  2. 您的图案包含特殊字符。使用静态方法[regex]::escape来解释它。
  3. 不是真正的问题,但你有一个似乎不合适的尾随反复。