简体   繁体   English

Powershell split()vs -split-有什么区别?

[英]Powershell split() vs -split - what's the difference?

After struggling with this for half an hour, I've experienced this difference when splitting a string with spaces, depending on which syntax you use. 在苦苦挣扎半小时之后,我在使用空格分割字符串时遇到了这种差异,具体取决于您使用的语法。

Simple string: 简单字符串:

$line = "1: 2: 3: 4: 5: "

Split example 1 - notice the extra space with tokens from 1 onwards: 拆分示例1-从1开始注意带有令牌的额外空间:

$ln = $line.split(":\s+")
$ln
1
 2
 3
 4
 5

Split example 2 - the spaces are gone (as they should) 拆分示例2-空格消失(应有)

$ln = $line -split ":\s+"
$ln
1
2
3
4
5

I suspect it's because the first one is a .NET method (?), and the -split PS operator perhaps has more baked-in smarts when it comes to regex interpretation. 我怀疑这是因为第一个是.NET方法(?),并且-split PS运算符在进行正则表达式解释时可能具有更多的功能。

However, when I tried the first method with the split like ": " , that didn't work properly either. 但是,当我尝试使用第一个方法如": "进行拆分时,该方法也无法正常工作。 If it's .NET, does it need something to correctly interpret the fact it should be using both characters as a delimiter? 如果是.NET,是否需要一些东西来正确解释它应该同时使用两个字符作为分隔符的事实?

The .Net System.String.Split method does not have an overload that takes a single parameter that is a string. .Net System.String.Split方法没有使用单个参数(字符串)的重载。 It also does not understand regex. 它还不了解正则表达式。

What is happening is that powershell is taking the string you are passing in and converting it to an array of characters. 发生的情况是,powershell正在接收您要传递的字符串,并将其转换为字符数组。 It is essentially splitting at the following characters : , \\ , s , + 它实际上是拆分为以下字符:\\s+

When you use ": " as the delimiter, I would imagine you got results like 当您使用": "作为分隔符时,我想您会得到如下结果

1

2

3

4

5

That is because without specifying a string split option to the .Net method, it will include empty strings that it finds between adjacent separators. 这是因为没有为.Net方法指定字符串拆分选项,它将包含在相邻分隔符之间找到的空字符串。

  1. .Net object does not take regex but interprets it as a literal string. .Net对象不使用正则表达式,而是将其解释为文字字符串。

     $line = "1: 2: 3: 4: 5: " $ln = $line.split("\\s") $ln 

Output: 输出:

1: 2: 3: 4: 5: 

Hence, in your example, the "+" is ignored as it it not found in $ln but ":\\s" is used for splitting: 因此,在您的示例中,“ +”被忽略,因为它在$ ln中找不到,但“:\\ s”用于拆分:

$ln = $line.split(":\s+")
$ln

Output: 

1
 2
 3
 4
 5

which is same as 与...相同

$ln = $line.split(":")
$ln

2.while powershell -split operator interprets "\\s" as a valid regex ie, space. 2.while powershell -split运算符将“ \\ s”解释为有效的正则表达式,即空格。 Since, it can find both : and \\s in the string, the combination of ":\\s" is used for splitting. 由于它可以在字符串中找到:和\\ s,因此使用“:\\ s”的组合进行拆分。 eg: 例如:

$line = "1:2:3: 4: 5: "
$ln = $line -split ":"
$ln

output: 输出:

1
2
3
 4
 5

The string split method takes a character array argument (not a string). 字符串拆分方法采用字符数组参数(而不是字符串)。 If you specify multiple characters it will split on any instance of any of those characters. 如果指定多个字符,它将在任何这些字符的任何实例上分割。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM