Powershell 用一个替换表达式替换多个变量字符串
如何使用字符串替换替换多个可变字符串?我目前正在这样做:Powershell 用一个替换表达式替换多个变量字符串,powershell,powershell-4.0,Powershell,Powershell 4.0,如何使用字符串替换替换多个可变字符串?我目前正在这样做: > $a = "a" > $b = "b" > $c = "c" > $d = "d" > $e = "e" > $f = "f" > $g = "g" > "abcdefg" -replace $a -replace $b -replace $c -replace $d -replace $e -replace $f -replace $g 只有一个-replace语
> $a = "a"
> $b = "b"
> $c = "c"
> $d = "d"
> $e = "e"
> $f = "f"
> $g = "g"
> "abcdefg" -replace $a -replace $b -replace $c -replace $d -replace $e -replace $f -replace $g
只有一个-replace
语句,我们怎么能做到这一点呢?像这样
$a = "a"
$b = "b"
$c = "c"
$d = "d"
$e = "e"
$f = "f"
$g = "g"
$regex = $a,$b,$c,$d,$e,$f,$g -join '|'
"abcdefg" -replace $regex
像这样
$a = "a"
$b = "b"
$c = "c"
$d = "d"
$e = "e"
$f = "f"
$g = "g"
$regex = $a,$b,$c,$d,$e,$f,$g -join '|'
"abcdefg" -replace $regex
可以使用列表和foreach循环。那你只需要写一次
$str = 'abcdefg'
$replacements = $a,$b,$c,$d,$e,$f,$g
foreach ($r in $replacements) {
$str = $str -replace $r
}
$str
可以使用列表和foreach循环。那你只需要写一次
$str = 'abcdefg'
$replacements = $a,$b,$c,$d,$e,$f,$g
foreach ($r in $replacements) {
$str = $str -replace $r
}
$str
以下是我能想到的最接近的方法(无需专门为任务定义函数):
cls
$replacements = @(@("a","z"),@("b","y"),@("c","x"))
$text = "abcdef"
$replacements | %{$text = $text -replace $_[0],$_[1]; $text} | select -last 1
更新
但是,如果您愿意使用函数,可以尝试以下方法:
cls
function Replace-Strings
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$True,ValueFromPipeline=$true,Position=1)]
[string] $string
,[Parameter(Mandatory=$True,Position=2)]
[string[]] $oldStrings
,[Parameter(Mandatory=$false,Position=3)]
[string[]] $newStrings = @("")
)
begin
{
if ($newStrings.Length -eq 0) {$newStrings = "";}
$i = 0
$replacements = $oldStrings | %{Write-Output @{0=$_;1=$newStrings[$i]}; $i = ++$i % $newStrings.Length;}
}
process
{
$replacements | %{ $string = $string -replace $_[0], $_[1]; $string } | select -last 1
}
}
#A few examples
Replace-Strings -string "1234567890" -oldStrings "3"
Replace-Strings -string "1234567890" -oldStrings "3" -newStrings "a"
Replace-Strings -string "1234567890" -oldStrings "3","5"
Replace-Strings -string "1234567890" -oldStrings "3","5" -newStrings "a","b"
Replace-Strings -string "1234567890" -oldStrings "1","4","5","6","9" -newStrings "a","b"
#Same example using positional parameters
Replace-Strings -string "1234567890" "1","4","5","6","9" "a","b"
#or you can take the value from the pipeline (you must use named parameters if doing thi)
"1234567890" | Replace-Strings -oldStrings "3"
"1234567890","123123" | Replace-Strings -oldStrings "3"
"1234567890","1234123" | Replace-Strings -oldStrings "3","4","1" -newStrings "X","Y"
输出:
124567890
12a4567890
12467890
12a4b67890
a23bab78a0
a23bab78a0
124567890
124567890
1212
X2XY567890
X2XYX2X
以下是我能想到的最接近的方法(无需专门为任务定义函数):
cls
$replacements = @(@("a","z"),@("b","y"),@("c","x"))
$text = "abcdef"
$replacements | %{$text = $text -replace $_[0],$_[1]; $text} | select -last 1
更新
但是,如果您愿意使用函数,可以尝试以下方法:
cls
function Replace-Strings
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$True,ValueFromPipeline=$true,Position=1)]
[string] $string
,[Parameter(Mandatory=$True,Position=2)]
[string[]] $oldStrings
,[Parameter(Mandatory=$false,Position=3)]
[string[]] $newStrings = @("")
)
begin
{
if ($newStrings.Length -eq 0) {$newStrings = "";}
$i = 0
$replacements = $oldStrings | %{Write-Output @{0=$_;1=$newStrings[$i]}; $i = ++$i % $newStrings.Length;}
}
process
{
$replacements | %{ $string = $string -replace $_[0], $_[1]; $string } | select -last 1
}
}
#A few examples
Replace-Strings -string "1234567890" -oldStrings "3"
Replace-Strings -string "1234567890" -oldStrings "3" -newStrings "a"
Replace-Strings -string "1234567890" -oldStrings "3","5"
Replace-Strings -string "1234567890" -oldStrings "3","5" -newStrings "a","b"
Replace-Strings -string "1234567890" -oldStrings "1","4","5","6","9" -newStrings "a","b"
#Same example using positional parameters
Replace-Strings -string "1234567890" "1","4","5","6","9" "a","b"
#or you can take the value from the pipeline (you must use named parameters if doing thi)
"1234567890" | Replace-Strings -oldStrings "3"
"1234567890","123123" | Replace-Strings -oldStrings "3"
"1234567890","1234123" | Replace-Strings -oldStrings "3","4","1" -newStrings "X","Y"
输出:
124567890
12a4567890
12467890
12a4b67890
a23bab78a0
a23bab78a0
124567890
124567890
1212
X2XY567890
X2XYX2X