匹配C#中字典的正则表达式

Chr*_*ken 7 c# regex dictionary match

我正在尝试使用某种数据对象(我在想字典)来保存正则表达式的TON作为键,然后我需要取一串文本,然后匹配它们以从字典中获取实际值.我需要一种有效的方法来处理大量数据.

我在C#,我不知道从哪里开始.

gpm*_*dam 9

为什么不使用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.
Run Code Online (Sandbox Code Playgroud)