我正在考虑这样的列表列表。
var myList = new List<List<string>>();
myList.Add(new List<string> { "#16", "100%", "Monitor" });
myList.Add(new List<string> { "#19", "98%", "Guide" });
myList.Add(new List<string> { "#77", "0%", "Cord" });
来自这样的代码。
string mySource = @"
#16
100%
Monitor
#19
98%
Guide
#77
0%
Cord
";
var myPattern = @"#(\d+)\r\n(\d+)%\r\n([^\r\n]*)\r\n";
var myCollection = Regex.Matches(mySource, myPattern, RegexOptions.Singleline)
.Cast<Match>()
.Select(x => Regex.Split(x.Value, "\r\n"))
.ToList();
显然位于&#34;匹配&#34;它有一个数组。
如何将其更改为列表列表?
此致
答案 0 :(得分:1)
只需使用ToList
扩展程序Array
转换为List
string mySource = @"
#16
100%
Monitor
#19
98%
Guide
#77
0%
Cord
";
var myPattern = @"#(\d+)\r\n(\d+)%\r\n([^\r\n]*)\r\n";
var myCollection = Regex.Matches(mySource, myPattern, RegexOptions.Singleline)
.Cast<Match>()
.Select(x => Regex.Split(x.Value, "\r\n").ToList())
.ToList();
答案 1 :(得分:1)
显然,在“匹配”的中间,它有一个数组。
显然你必须把它转换成一个列表。
//.Select(x => Regex.Split(x.Value, "\r\n"))
.Select(x => Regex.Split(x.Value, "\r\n").ToList())
var pattern = string.Join(@"\r\n", @"(?<id>#\d+)", @"(?<percent>\d+%)", @"(?<name>\w+)");
Regex.Matches(mySource, pattern, RegexOptions.Singleline)
.Cast<Match>()
.Select(m => new[] //or you can use : m => new List<string>
{
m.Groups["id"].Value,
m.Groups["percent"].Value,
m.Groups["name"].Value
})
.ToList();