Table of Contents
Using -split
Operator with Regular Expression
Use the -split
operator with a regular expression to split string by multiple delimiters in PowerShell.
1 2 3 4 5 6 |
$string = "one,two;three four" $delimiters = "[,; ]+" $parts = $string -split $delimiters $parts |
1 2 3 4 5 6 |
one two three four |
First, a string variable named $string
and an array variable called $delimiters
is defined. Then, the $string
is assigned with "one,two;three four"
, while $delimiters
is set with "[,; ]+"
.
After that, the -split
operator is used on the string variable named $string
, with the $delimiters
variable passed as an argument. The resulting output is an array containing the individual parts of the string split by the specified delimiters.
In case you want to remove entries, you can use where-object cmdlet.
1 2 3 4 5 6 |
$string = "one,two;three four" $delimiters = "[,; ]+" $parts = $string -split $delimiters | Where-Object { $_ -ne "" } $parts |
1 2 3 4 5 6 |
one two three four |
In the above example, we used the Where-Object
cmdlet to select all substrings of the given array excluding empty elements (empty substrings). How to exclude empty elements? For that, we used a condition as {$_ -ne ""}
to determine if the current element ($_
) is not equal to (-ne
) an empty string (""
). Finally, the resulting array was stored in the $parts
variable.
Using Regex.Split()
Method
Use the Regex.Split()
method to split string by multiple delimiters in PowerShell.
1 2 3 4 5 6 |
$string = "one,two;three four" $delimiters = "[,; ]+" $parts = [Regex]::Split($string, $delimiters) $parts |
1 2 3 4 5 6 |
one two three four |
First, the PowerShell variable $string
is assigned a with string, i.e., "one,two;three four"
and the delimiters "[,; ]+"/
are assigned to the $delimiters
variable.
After that, to split the string, the Regex.Split()
method is used in this solution. Finally, the $string
and the $delimiters
variables are given as parameters to the Split()
method. The resulting output also being an array containing the individual parts of the string.
Using String.Split()
Method
Use the String.Split()
method to split string by multiple delimiters in PowerShell.
1 2 3 4 5 6 |
$string = "one,two;three four" $delimiters = @(",", ";", " ") $parts = $string.Split($delimiters, [System.StringSplitOptions]::RemoveEmptyEntries) $parts |
1 2 3 4 5 6 |
one two three four |
The code defines a string variable named $string
and an array variable called $delimiters
. The string contains a sequence of words separated by commas, semicolons, and spaces, i.e., "one,two;three four"
. The array includes the delimiters that should be used to split the string, i.e.,@(",", ";", " ")
. Then the Split()
method is called on the string variable $string
, passing the $delimiters
array as the delimiter parameter.
The RemoveEmptyEntries
option removes any empty entries resulting from consecutive delimiters or delimiters at the beginning or end of the string. The resulting array is stored in the $parts
variable and then displayed.
Further reading:
Using System.Text.RegularExpressions.Regex
Class
Use the System.Text.RegularExpressions.Regex
class to split strings by multiple delimiters in PowerShell.
1 2 3 4 5 6 7 |
$string = "one,two;three four" $delimiters = "[,; ]+"/ $regex = [System.Text.RegularExpressions.Regex]::new($delimiters) $parts = $regex.Split($string) $parts |
1 2 3 4 5 6 |
one two three four |
First, the PowerShell variable $string
is assigned a with string, i.e., "one,two;three four"
. After that the delimiters "[,; ]+"/
are assigned to the $delimiters
variable. Then, an instance of the Regex
class is created. The $delimiters
variable is passed as an argument to the new()
function of the System.Text.RegularExpressions.Regex
class.
Then the Regex
instance is used with the Split()
method to split the string. The resulting output also being an array containing the individual parts of the string. Note that all the methods discussed above generated the same outcome. You may choose any of the methods as per your requirement and preference.
That’s all about powershell split string by multiple delimiters.