gpt4 book ai didi

xml - 无法替换PowerShell中的字符串

转载 作者:行者123 更新时间:2023-12-02 23:58:24 26 4
gpt4 key购买 nike

            if ("$string" -match "$key")
{
Write-Host "Attempting to replace $string with $value in $sourcefilename"

(Get-Content $sourcefilename).Replace("{{$string}}",$value) | set-content $destinationfilename

}
}
有人可以告诉我如何用字符串替换各个值。

最佳答案

下次您提问题时,请确定您的问题。
您要替换字典的键值对中的文本。

您的错误是,您总是阅读$sourcefilename,替换了文本并写入了destinationfilename。可能只有字典的最后一个条目被替换了。

因此,您只需阅读一次内容,然后遍历字典并替换值:

$templatecontent = Get-Content $sourcefilename
$Dictionary.Keys | % { $templatecontent = $templatecontent -replace "{{$_}}", ($Dictionary[$_]) }
templatecontent | set-content $destinationfilename

关于xml - 无法替换PowerShell中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36973464/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com