Mrutyunjaya Mohapatra
Mrutyunjaya Mohapatra

Reputation: 59

Split a string to multiple strings in powershell

I have strings like

$value = "1;#Mohapatra,Mrutyunjaya (ADM) 10;#Sumit Upadhyay(ADM) 11;#Naidu,Ishan(ADM)"

I want to retrieve

"Mohapatra,Mrutyunjaya (ADM)", "Sumit Upadhyay(ADM)", "Naidu,Ishan(ADM)" 

from $value.

I have tried $value.Split(";#")[0]. It is returning the first parameter only. But I want all the parameters

Upvotes: 0

Views: 404

Answers (2)

Nate
Nate

Reputation: 862

Just FYI, if you want to declare each as a variable you can say $a,$b,$c,$d = $Value -Split (";#") and each of $a, $b, $c and $d will retain those values.

Upvotes: 1

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200463

Split your string at sequences of \s*\d+;# (optional whitespace, followed by a number, a semicolon, and a hash character), and remove empty elements from the resulting list:

$value -split '\s*\d+;#' | Where-Object { $_ }

Upvotes: 3

Related Questions