Since there was no such solution, using only LINQ, and I was wondering what it would look like, I came up with this. But I would not recommend using it in production code. Actually, I was hoping this would be better, but since the nested parent elements needed to be processed, I had to introduce state variable variables.
string data = "P,MV,A1ZWR,MAV#(X,,), PV,MOV#(X,12,33),LO";
int depth = 0;
int group = 0;
var result = data
.GroupBy(x => {
if (x == '(') depth++;
if (x == ')') depth--;
if (x == ',' && depth == 0) group++;
return group; })
.Select(x => new String(x.ToArray()).Trim(' ', ','))
source
share