# powershell -v 2 -c .\replace.ps1
$dic = @{
'${TEST1}' = 'テスト1';
'${test2}' = 'テスト2';
}
$content = @(
'--テスト--',
'--${TEST1}:${TEST2}:${test1}:${test2}--')
$vars = @()
[RegEx]::Matches($content, '\${[^}]+}') | ForEach-Object {
if ($vars -contains $_.Value) { return }
$vars += $_.Value
}
$content | ForEach-Object {
Write-Host $_
}
$vars | ForEach-Object {
$content = $content -replace [Regex]::Escape($_), $dic.$_
}
$content | ForEach-Object {
Write-Host $_
}
実行結果
--テスト--
--${TEST1}:${TEST2}:${test1}:${test2}--
--テスト--
--テスト1:テスト2:テスト1:テスト2--
function Rewrite($key, $value, $filename, $outfilename) {
Get-Content $filename |
ForEach-Object { $_ -replace "^\[item $($key)\(""[^""]*""\)\]$", "[item $($key)(""$($value)"")]" } |
Set-Content $outfilename
}
Rewrite "Key" "TEST" file1.cs file2.cs