string 如何在PowerShell中使用运算符“-replace”将文本字符串替换为特殊字符并成功替换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19794187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:08:57  来源:igfitidea点击:

How to use operator '-replace' in PowerShell to replace strings of texts with special characters and replace successfully

stringpowershellreplace

提问by user2957157

I have a script where I am basically doing a find and replace on several strings of text. The first couple of strings work, but when I do the account keys, they do not. How can I fix this problem?

我有一个脚本,我基本上是在其中对几个文本字符串进行查找和替换。前几个字符串有效,但是当我处理帐户密钥时,它们不起作用。我该如何解决这个问题?

Here is the script:

这是脚本:

Get-ChildItem "[FILEPATH]" -recurse |
    Foreach-Object {
        $c = ($_ | Get-Content)
        $c = $c -replace 'abt7d9epp4','w2svuzf54f'
        $c = $c -replace 'AccountName=adtestnego','AccountName=zadtestnego'
        $c = $c -replace 'AccountKey=eKkij32jGEIYIEqAR5RjkKgf4OTiMO6SAyF68HsR/Zd/KXoKvSdjlUiiWyVV2+OUFOrVsd7jrzhldJPmfBBpQA==','DdOegAhDmLdsou6Ms6nPtP37bdw6EcXucuT47lf9kfClA6PjGTe3CfN+WVBJNWzqcQpWtZf10tgFhKrnN48lXA=='
        [IO.File]::WriteAllText($_.FullName, ($c -join "`r`n"))
    }

回答by Adil Hindistan

'-replace' does a regex search and you have special characters in that last one (like +) So you might use the non-regex replace version like this:

'-replace' 进行正则表达式搜索,最后一个中有特殊字符(如 +),因此您可以使用非正则表达式替换版本,如下所示:

$c = $c.replace('AccountKey=eKkij32jGEIYIEqAR5RjkKgf4OTiMO6SAyF68HsR/Zd/KXoKvSdjlUiiWyVV2+OUFOrVsd7jrzhldJPmfBBpQA==','DdOegAhDmLdsou6Ms6nPtP37bdw6EcXucuT47lf9kfClA6PjGTe3CfN+WVBJNWzqcQpWtZf10tgFhKrnN48lXA==')

回答by mjolinor

If you've got V3, you can take advantage of auto-enumeration, the -Raw switch in Get-Content, and some of the new line contiunation syntax to simply it to this, using the string .replace() method instead of the -replace operator:

如果你有 V3,你可以利用自动枚举、Get-Content 中的 -Raw 开关和一些新的行连续语法来简单地使用字符串 .replace() 方法而不是-替换运算符:

(Get-ChildItem "[FILEPATH]" -recurse).FullName |
  Foreach-Object {
   (Get-Content $_ -Raw).
     Replace('abt7d9epp4','w2svuzf54f').
     Replace('AccountName=adtestnego','AccountName=zadtestnego').
     Replace('AccountKey=eKkij32jGEIYIEqAR5RjkKgf4OTiMO6SAyF68HsR/Zd/KXoKvSdjlUiiWyVV2+OUFOrVsd7jrzhldJPmfBBpQA==','AccountKey=DdOegAhDmLdsou6Ms6nPtP37bdw6EcXucuT47lf9kfClA6PjGTe3CfN+WVBJNWzqcQpWtZf10tgFhKrnN48lXA==') |
   Set-Content $_
  }

Using the .replace() method uses literal strings for the replaced text argument (not regex), so you don't need to worry about escaping regex metacharacters in the text-to-replace argument.

使用 .replace() 方法将文字字符串用于替换文本参数(不是正则表达式),因此您无需担心在 text-to-replace 参数中转义正则表达式元字符。

回答by Anthony Neace

In your example, you prepended your source string with AccountKey=but not your target string.

在您的示例中,您在源字符串之前添加了AccountKey=目标字符串,但未添加目标字符串。

$c = $c -replace 'AccountKey=eKkij32jGEIYIEqAR5RjkKgf4OTiMO6SAyF68HsR/Zd/KXoKvSdjlUiiWyVV2+OUFOrVsd7jrzhldJPmfBBpQA==','AccountKey=DdOegAhDmLdsou6Ms6nPtP37bdw6EcXucuT47lf9kfClA6PjGTe3CfN+WVBJNWzqcQpWtZf10tgFhKrnN48lXA=='

By not including that in the target string, the resulting string will remove AccountKey=instead of replacing it. You correctly do this with the AccountName=example, which seems to support this conclusion since it is not giving you any problems. If you really mean to have that prepended, then this may resolve your issue.

通过不在目标字符串中包含它,生成的字符串将被删除AccountKey=而不是替换它。你用这个AccountName=例子正确地做到了这一点,它似乎支持这个结论,因为它没有给你任何问题。如果你真的想把它放在前面,那么这可能会解决你的问题。