在powershell中删除一个以上的空白

时间:2021-09-04 20:11:56

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

我试图在powershell中找到一种方法来删除多个空格。

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

但是我发现如何在php中实现。“删除一个以上空白”

There will be similar regular expression may available .

可能会有类似的正则表达式可用。

How to acheive the same in powershell?

如何在powershell中使用相同的方法?

My string is like this

我的弦是这样的

Xcopy Source  Desination

Some lines may contain more than one white space between Source and destination.

有些行可能包含源和目标之间的多个空格。

1 个解决方案

#1


48  

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

如果您希望将多个连续空格字符合并到一个空格中,那么可以使用-replace操作符:

'[     Hello,     World!     ]' -replace '\s+', ' '

...returns...

返回…

[ Hello, World! ]

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. Enter help about_comparison_operators or see here for more information.

要替换的第一个参数是要匹配的正则表达式模式,第二个参数是要替换任何匹配的文本。\s将匹配一个空格字符,+表示匹配一个或多个出现,因此,换句话说,一个或多个相邻的空格字符将被替换为一个空格。输入help about_compare on_operators或在这里查看更多信息。

#1


48  

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

如果您希望将多个连续空格字符合并到一个空格中,那么可以使用-replace操作符:

'[     Hello,     World!     ]' -replace '\s+', ' '

...returns...

返回…

[ Hello, World! ]

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. Enter help about_comparison_operators or see here for more information.

要替换的第一个参数是要匹配的正则表达式模式,第二个参数是要替换任何匹配的文本。\s将匹配一个空格字符,+表示匹配一个或多个出现,因此,换句话说,一个或多个相邻的空格字符将被替换为一个空格。输入help about_compare on_operators或在这里查看更多信息。