【问题标题】:Replace many variable strings with one -replace expression用一个 -replace 表达式替换多个变量字符串
【发布时间】:2015-05-11 06:55:39
【问题描述】:

如何使用字符串替换来替换多个可变字符串?我目前正在这样做:

 > $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 语句来做到这一点?

【问题讨论】:

    标签: powershell powershell-4.0


    【解决方案1】:

    像这样?

    $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
    

    【讨论】:

      【解决方案2】:

      您可以使用列表和 foreach 循环。然后你只需要写一次。

      $str = 'abcdefg'
      $replacements = $a,$b,$c,$d,$e,$f,$g
      
      foreach ($r in $replacements) {
        $str = $str -replace $r
      }
      
      $str
      

      【讨论】:

        【解决方案3】:

        这是我能想到的最接近的(没有专门为任务定义函数:

        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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-04-12
          • 1970-01-01
          • 2022-11-20
          • 2010-11-19
          • 1970-01-01
          相关资源
          最近更新 更多