regex - Split a complex string into a list of classes using regular expressions and lambda in C# -
given string "a:b,c", use regular expressions , lambda expressions in c# split string list of classes return following.
{ column1: "a", column2: "b", }, { column1: "a", column2: "c", }
in other words, want repeat value before colon every comma separated value behind colon. in sql equivalent of doing cross join column1 left side of join , column2 right side.
i have of code, including regular expressions, cannot second select
project split
of comma separated values new classes. instead, code returns following.
{ column1: "a", column2: [ "b", "c" ] }
here's code, stands.
public class myclass { public string column1 { get; set; } public string column2 { get; set; } } list<myclass> mc = "a:b,c" .select(a => new { column1 = new regex(@"[a-z]+(?=\:)").match(a).value), column2s = new regex(@"(?<=\:)[a-z]+(,[a-z]+)*").match(a).value }) .select(b => new myclass { column1 = b.column1, column2 = b.column2s.split(',') }) .tolist();
thank help.
string input = "a:b,c"; int colon = input.indexof(':'); string left = input.substring(0, colon); string right = input.substring(colon + 1); list<myclass> result = right.split(',') .select(x => new myclass { column1 = left, column2 = x, }) .tolist();
Comments
Post a Comment