替换包含 $ 字符的字符串

Replacing Strings Containing $ Characters

我正在尝试开发一个脚本,用新的测试字符串替换输入文本中的某些标记。在 this 的帮助下,我开发了以下内容:

$repl = @{}
$repl.add('SVN',"myworkspace\BRANCH71")
$repl.add('REL',"72")

$string = 'C:\users\rojomoke\filesREL\SVN\blah.txt'
foreach ($h in $repl.getenumerator())
{
    write-host "Line: $($h.name): $($h.value)"
    $string = $string -replace "$($h.name)","$($h.value)"
    write-host $string
}

生成所需的 C:\users\rojomoke\files72\myworkspace\BRANCH71\blah.txt.

但是,当我尝试使用以 $ 符号开头标记的标记时,这一切都归于 sh^H^Hpieces。如果在上面的示例中我使用标记 $REL$SVN,则不会发生替换,并且 $string 仍然是 C:\users\rojomoke\files$REL$SVN\blah.txt.

我假设我 运行 与正则表达式扩展或其他东西有冲突,但我看不出如何。是否可以引用美元符号以使其正常工作?

我正在使用 Powershell 版本 3。

-replace 运算符使用正则表达式匹配。 $ 字符在正则表达式 ("end of string") 中具有特殊含义,其他一些字符也是如此。为避免这种情况,您必须在搜索字符串中转义这些字符:

$srch = [regex]::Escape('$SVN')
$repl = 'myworkspace\BRANCH71'

$string = 'C:\users\rojomoke\filesREL$SVN\blah.txt'

$string -replace $srch, $repl

但是,如果您无论如何都使用类似变量的语法,为什么不直接使用变量呢?

$repl = @{
  'SVN' = 'myworkspace\BRANCH71'
  'REL' = '72'
}

$repl.GetEnumerator() | % { New-Variable -Name $_.Name -Value $_.Value }

$string = "C:\users\rojomoke\files$REL$SVN\blah.txt"

如果需要在定义嵌套变量之前定义$string,可以用单引号定义字符串,稍后再求值:

$repl = @{
  'SVN' = 'myworkspace\BRANCH71'
  'REL' = '72'
}

$repl.GetEnumerator() | % { New-Variable -Name $_.Name -Value $_.Value }

$string = 'C:\users\rojomoke\files$REL$SVN\blah.txt'

$expandedString = $ExecutionContext.InvokeCommand.ExpandString($string)

-replace 将第一个参数视为正则表达式模式。在正则表达式中,$ 是一个特殊字符,表示字符串的最后一个字符位置 ("end")。因此,当试图匹配字符串中的文字字符 $ 时,需要对其进行转义。

您可以为此使用 [regex]::Escape($pattern)

$repl = @{}
$repl.add([regex]::Escape('$SVN'),"myworkspace\BRANCH71")
$repl.add([regex]::Escape('$REL'),"72")
$repl = @{}
$repl.add('$SVN',"myworkspace\BRANCH71")
$repl.add('$REL',"72")

$string = 'C:\users\rojomoke\files$REL$SVN\blah.txt'
foreach ($h in $repl.getenumerator()) {
    write-host "Line: $($h.name): $($h.value)"
    $string = $string -replace "$($h.name)","$($h.value)"
    write-host $string
}

之所以可行,是因为在正则表达式中,您必须使用正则表达式转义字符 \.

来转义 $