我使用EF核心1.2在那里我阅读textarea的输入,并检查各行,如果它符合我的模式之一在C#这下面的代码。 检查一行后,我试图设置一个状态,告诉我它是否匹配一个模式。现在如何将字符串加上其状态保存到List中?
我想这些线路临时加上它的每一个状态保存到列表,这样我就可以通过这个列表,我的看法,在这里我想显示在一个表中的每一行加上其状态。
我的问题是如何保存的每一行加上它的状态?我想过使用字典,但我不确定这是否是我的问题的最佳解决方案。
using (StringReader reader = new StringReader(Request.Form["ExpressionTextarea"].ToString()))
{
string line = string.Empty;
do
{
line = reader.ReadLine();
if (line != null)
{
string state = CheckStringLine(line);
/**** HOW TO SAVE EACH LINE PLUS ITS STATE TEMPORARILY?
//IDictionary<string, string> dictionary = new Dictionary<string, string>();
//dictionary.Add(line, status);
****/
}
} while (line != null);
//***PASSING MY LIST TO MY VIEW
return View(MYLIST);
}
//Checks if line matches a pattern
public string CheckStringLine(string Line)
{
string state = "";
//Pattern1: (Ein | Eine) A ist (ein | eine) B.
string pattern1 = @"^(?<Artikel1>(Ein|Eine){1})\s{1}(?<Second>[A-Z]{1}[a-zäöüß]{1,})\s{1}ist\s{1}(?<Artikel2>(eine|ein){1})\s(?<Fourth>[A-Z]{1}[a-zäöüß]{1,})\.$";
//Pattern2: (Ein | Eine) A (oder (ein | eine) B)+ ist (ein | eine) C.
string pattern2 = @"^(?<First>(Ein|Eine){1})\s{1}(?<Second>[A-Z]{1}[a-zäöüß]{1,})(\s{1}oder\s{1}(?<OptionalArtikel>(ein|eine){1})\s{1}(?<OptionalBegriff>[A-Z]{1}[a-zäöüß]{1,}))+(\s{1})ist\s{1}(?<Third>(eine|ein){1})\s(?<Fourth>[A-Z]{1}[a-zäöüß]{1,})\.$";
var match1 = Regex.Match(Line, pattern1);
var match2 = Regex.Match(Line, pattern2);
if (match1.Success)
{
state = "This Line is using pattern1";
return state;
}
if (match2.Success)
{
state = "This Line is using pattern2";
return state;
}
state = "No matches";
return state;
}
字典只要每个密钥都是唯一听起来不错。否则看看C#7的元组数据类型 –