Removing more than one white space in powershell

后端 未结 3 1073
执念已碎
执念已碎 2021-01-01 14:53

I was trying to find a way in powershell to remove more than one white space.

But what i found is how to do it in php. \"Removing more than one white-space\"

相关标签:
3条回答
  • 2021-01-01 14:57
    '[     Hello,     World!     ]' -replace '^\[\s+','[' -replace '\s+]$',']' -replace '\s+',' '
    

    Bacon has the explanation powershell is funny. I had to escape '[' with '\' even though I know powershells escape character is `.

    0 讨论(0)
  • 2021-01-01 15:18

    Another way. -split on the left side splits on variable white space:

    -split 'Xcopy Source  Destination' -join ' '
    
    Xcopy Source Destination
    
    0 讨论(0)
  • 2021-01-01 15:23

    If you're looking to collapse multiple consecutive whitespace characters into a single space then you can do this using the -replace operator. Given...

    PS> $beforeReplace = '   [   Hello,   World!   ]   '
    PS> $beforeReplace
       [   Hello,   World!   ]   
    PS> $beforeReplace.Length
    29
    

    ...you would call the -replace operator like this...

    PS> $afterReplace = $beforeReplace -replace '\s+', ' '
    PS> $afterReplace
     [ Hello, World! ] 
    PS> $afterReplace.Length
    19
    

    The first parameter to -replace is a regular expression pattern to match, and the second parameter is the text that will replace any matches. \s will match a whitespace character, and + indicates to match one or more occurrences, so, in other words, one or more adjacent whitespace characters will be replaced with a single space.

    Replacement without whitespace normalization

    If you don't need to normalize all whitespace characters to spaces and, thus, it's ok for standalone whitespace characters to be left untouched, then for long strings you might see better performance with this variation...

    PS> $afterReplace = $beforeReplace -replace '\s{2,}', ' '
    PS> $afterReplace
     [ Hello, World! ] 
    PS> $afterReplace.Length
    19
    

    The \s{2,} uses a quantifier meaning "match the preceding element at least two times"; therefore, standalone whitespace characters will not be replaced. When the input string contains a mix of whitespace characters...

    PS> $beforeReplace = "1Space: ;2Space:  ;1Tab:`t;2Tab:`t`t;1Newline:`n;2Newline:`n`n;"
    PS> $beforeReplace
    1Space: ;2Space:  ;1Tab:    ;2Tab:      ;1Newline:
    ;2Newline:
    
    ;
    PS> $beforeReplace.Length
    57
    

    ...note how the results for the two approaches differ...

    PS> $afterReplaceNormalized = $beforeReplace -replace '\s+', ' '
    PS> $afterReplaceNormalized
    1Space: ;2Space: ;1Tab: ;2Tab: ;1Newline: ;2Newline: ;
    PS> $afterReplaceNormalized.Length
    54
    PS> $afterReplaceUnnormalized = $beforeReplace -replace '\s{2,}', ' '
    PS> $afterReplaceUnnormalized
    1Space: ;2Space: ;1Tab: ;2Tab: ;1Newline:
    ;2Newline: ;
    PS> $afterReplaceUnnormalized.Length
    54
    

    While both yield strings of the same length, the unnormalized replacement leaves the single space, single tab, and single newline whitespace runs unmodified. This would work just the same whether adjacent whitespace characters are identical or not.

    Additional documentation

    • Enter help about_Comparison_Operators [ Windows PowerShell 2.0 ] [ PowerShell (Core) ]
    • Enter help about_Regular_Expressions [ Windows PowerShell 2.0 ] [ PowerShell (Core) ]
    • .NET Regular Expression Language - Quick Reference
    0 讨论(0)
提交回复
热议问题