我正在尝试使用某种数据对象(我在想字典)来保存正则表达式的TON作为键,然后我需要取一串文本,然后匹配它们以从字典中获取实际值.我需要一种有效的方法来处理大量数据.
@H_301_2@我在C#,我不知道从哪里开始.
解决方法
为什么不使用LINQ?
Dictionary<string,string> myCollection = new Dictionary<string,string>(); myCollection.Add("(.*)orange(.*)","Oranges are a fruit."); myCollection.Add("(.*)apple(.*)","Apples have pips."); myCollection.Add("(.*)dog(.*)","Dogs are mammals."); // ... string input = "tell me about apples and oranges"; var results = from result in myCollection where Regex.Match(input,result.Key,RegexOptions.Singleline).Success select result; foreach (var result in results) { Console.WriteLine(result.Value); } // OUTPUT: // // Oranges are a fruit. // Apples have pips.