这是使用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);
}
不能将其存储到Array中吗?在这里我只能显示我想要的项目。就像这里的输出是{Index =,Value =,Token =}同时,我希望不需要只是“ Value”“ Token”索引的输出。
您可以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);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句