Match Regular expression from a dictionary in C #

I am trying to have some kind of data object (I think a dictionary) to hold TON of regular expressions as keys, then I need to take a line of text and match them to get the actual value from the Dictionary. I need an efficient way to do this for a large dataset.

I'm in C # and I'm not sure where to start.

+5
source share
5 answers

Why not use 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.
+7
source

, - trie. trie. ( , , , " " ).

0

? ? , , , APPLY ?

, , . List StringCollection IndexOf (mytString), -1 , .

0

, , NFA ( -state automaton . , , .

. ( DFA, , NFA), . DFA, ( ) ( , ).

NFA . , ({( , )}), . NFA hash-consing, .

0

, , .

Regex RegexObject = new Regex(Pattern, RegexOptions.Compiled);

, , .

0

All Articles