在powershell中删除一个以上的空格 [英] Removing more than one white space in powershell

查看:78
本文介绍了在powershell中删除一个以上的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

但我发现的是如何在 php 中做到这一点."删除多个空格"

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 .

如何在powershell中实现相同的功能?

How to acheive the same in powershell?

我的字符串是这样的

Xcopy Source  Desination

某些行可能在源和目标之间包含多个空格.

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

推荐答案

如果您希望将多个连续的空白字符合并为一个空格,那么您可以使用 -replace 运算符来实现.鉴于...

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

...你会像这样调用 -replace 操作符...

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

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

-replace 的第一个参数是要匹配的正则表达式模式,第二个参数是将替换任何匹配项的文本.\s 将匹配一个空白字符,而 + 表示匹配一个或多个出现,因此,换句话说,一个或多个相邻的空白字符将被替换为单个空间.

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.

如果您不需要规范化所有空白字符为空格,因此,独立的空白字符可以保持不变,然后对于长字符串,您可能看到这种变体的性能更好...

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

\s{2,} 使用 量词 意思是匹配前面的元素至少两次";因此,不会替换独立的空白字符.当输入字符串包含混合空格字符时...

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.

  • 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

这篇关于在powershell中删除一个以上的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆