我使用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的元組數據類型 –