Removing more than one white space in powershell
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
Related videos on Youtube
Samselvaprabu
I am mainly working as a Build and integrator. I am maintaining ESXi servers and doing little bit of Configuration management activities (Base Clearcase and clearcase UCM) as every build and integrator.
Updated on November 28, 2020Comments
-
Samselvaprabu over 3 years
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"
There will be similar regular expression may available .
How to acheive the same in powershell?
My string is like this
Xcopy Source Desination
Some lines may contain more than one white space between Source and destination.
-
Samselvaprabu over 12 yearsThanks Mr.Bacon. It does the job for me.
-
Robert Allan Hennigan Leahy over 12 yearsUsing Trim(), TrimStart(), and/or TrimEnd() is an unequivocally better option.
-
Lance U. Matthews over 12 yearsFirstly, my answer is a working, alternative solution, so I don't see why it deserves a downvote. Secondly, the Trim*() methods operate specifically on the beginning and/or end of a string, whereas the author gave an example of a string where the spaces to be removed are in the middle of a string, which Trim*() would ignore.
-
Jay over 12 yearsUp-voted, as I agree with BACON's reason, regarding the limitation of "trim" command set. Second, this is a solution I will use.
-
travis over 8 years@RobertAllanHenniganLeahy Using
Trim()
,TrimStart()
, and/orTrimEnd()
is an unequivocally wrong answer, but also do you think that if it weren't, there would be a friendlier way stating that? -
Rob at TVSeries.com over 3 yearsWhile yes, the PS escape character is
`
that would only work inside"
double-quoted strings, not inside the'
single-quoted string in your example, which is actually regex syntax, where `\` is the escape character.