
Posted by Alfred Spector, Vice President, EngineeringRecently, at the 27th ACM User Interface Software and Technology Symposium (UIST’14), Google Senior...
When you run this it returns five values:"Lastname:FirstName:Address"-split "(:)"
The delimited is a character (or characters) that identify the end of the substring. The default is whitespace, which includes spaces and control characters, such as new line (`n) and tab (`t). Normally, the delimiter is omitted from the result set. To preserve part (or all) of it, enclose the part you want to retain in parentheses.Lastname:FirstName:Address
Note that I am using a multi-character delimiter. It is good to be aware that you are not limited to single-character delimiters. This outputs the following:test1234test1234test123 -split test
Now, here is an example of where you retain the delimiter, test, by placing it in quotes:12341234123
test1234test1234test123 -split (test)and its output:
NOTE: When providing an initial definition for -split I said, "In PowerShell, the general syntax is to place whatever you want to split on the left hand side of the operator, the -split operator in the middle and the delimiter on the right hand side of the operator". What I want to emphasize here is the condition in general. The operator has a second use where you can specify the maximum number of substrings after the delimiter.test1234test1234test123
a b c d e f -split ,3When you run this in PowerShell it returns this:
What is happening is PowerShell tokenizes the string, and, pops 1 (a), 2(b) and 3 (c d e f) character groups for the result. This is useful when you need to have a fixed number of results but cant control the input. Note that values of 0 or negative integers will return the full, original substring.abc de f
When it is run, it returns the following:$c = "Mercury,Venus,Earth,Mars,Jupiter,Saturn,Uranus,Neptune"$c -split{$_ -eq"e" -or$_ -eq"p"}
This can be very helpful if you have very specific conditions you need to meet, and/or criteria to match against. Additionally, you can perform dynamic analysis, i.e., calculations, with scriptblocks.Mrcury,Vnus,Earth,Mars,Juitr,Saturn,Uranus,Ntun
The syntax for handling this with -Split looks like this:$a = @1The first line.2The second line.3The third of three lines.@
$a -split"^d",0, "multiline"All we are trying to do with this regex is to return all substrings (with a maxsubstrings value of 0) and an option of "multiline". When it is run, it returns this:
The fourth key point about using -Split involves recognizing a difference between unary and binary splitting. Unary splitting, where there is no left hand side to the statement, has a higher precedence order than a binary split operator, where there are both left and right hand sides to a statement. To explain this, let us look at this example. For example, if we evaluate the following statement,The firstline.The secondline.The thirdof threelines.
-split "1 2", "a b"It returns this
Since my first exposure to -Split was the binary operator, this was a little confusing to me first. It seemed, in my initial understanding (binary operator only) you had to have both a Left Hand Side (LHS) and a Right Hand Side (RHS) to form a complete statement. For binary operation, this is true, but, not for unary operation.12a b
which evaluates to(1 2, 3 4) -split ,
Here are some other results. Splitting on nothing, (1 2, 3 4) -split , gives you,1 23 4
12