这是使用LINQ的代码,它将值存储到列表
中string StringRegex = "\"(?:[^\"\\\\]|\\\\.)*\"";
Dictionary<string, string> dictionaryofString = new Dictionary<string, string>()
{
{"String", StringRegex}
};
var matches = dictionaryofString.SelectMany(a => Regex.Matches(input,a.Value)
.Cast<Match>()
.Select(b =>
new
{
Index = b.Index,
Value = b.Value,
Token = a.Key
}))
.OrderBy(a => a.Index).ToList();
for (int i = 0; i < matches.Count; i++)
{
if (i + 1 < matches.Count)
{
int firstEndPos = (matches[i].Index + matches[i].Value.Length);
if (firstEndPos > matches[(i + 1)].Index)
{
matches.RemoveAt(i + 1);
i--;
}
}
}
foreach (var match in matches)
{
Console.WriteLine(match);
}
它可以不存储到数组中吗?在哪里我只能显示我想要的项目。就像这里输出的是{Index =,Value =,Token =} 同时我希望输出只是&#34; Value&#34; &#34;令牌&#34;索引不需要。
答案 0 :(得分:1)
您可以改用ToArray
。但是List
已经为您提供了所需的数组功能,您可以通过索引访问项目。
var exampleQuery = select c from componentsDemo;
var list = exampleQuery.ToList();
var secondElement = list[1]; // <- demo only, there could be an exception thrown, if there's less than two elements in the list
编辑:正如我从你的评论中看到的,你需要这个:
foreach (var match in matches)
{
Console.WriteLine(match.Value + ", " + match.Token);
}