我需要在C#中输入一些模式匹配多个模式作为输入

时间:2011-08-28 07:57:53

标签: c# linq

我需要做一些模式匹配来处理问题列表并提出 一些声明将列入解决方案列表。我需要匹配的每个问题 有第一句以“?”结尾。例如,如果有以下字符串:

input="Which of the following can be aa ... zz?"

然后我需要看看这是否与某些预定义模式匹配,如果确实如此,那么我需要填充两个变量。在此示例中,上面的字符串需要生成以下内容:

string1= "Can be aa ... zz:"
string2= "Cannot be contained aa ... zz:"

源中的文本aa ... zzz出现在目标中。

Inputs:

1 - Which of the following can be aa ... zz?
2 - Which of the following are correct?
3 - What will result when aa ... zz?
4 - Which of the following is a aa ... zz?
5 - Which are aa ... zz?
6 - What can be said about aa ... zz:
7 - Which of the following is true?
8 - What will be the result when aa ... zz?

Output 1:

1 - Can be aa ... zz:
2 - Correct:
3 - The following will result::
4 - A aa ... zz
5 - The following are aa ... zz
6 - True statement(s)
7 - True statement(s)
8 - The following will result:

Output 2:

1 - Cannot be aa ... zz:
2 - Incorrect:
3 - The following will not result
4 - Not a aa ... zz:
5 - Are not aa ... zz:
6 - False statement(s)
7 - False statement(s)
8 - The following will not result

我认为我应该将输入模式和输出模式存储在参考数据类中,然后可能是链接和模式匹配的某种组合。

有没有人有任何建议。即使我能看到如何做一场比赛也不错。 如果我有一个例子,那么我可以编写更多代码并从中开始工作。

什么样的模式? - 模式是文本。例如,我希望看看我的输入字符串是否以“以下哪个可以”开头,并以“?”结尾。

这些问题来自哪里? - 一组有限的问题。如果我不能得到一个匹配,那么我会在输出中加入类似“真”或“假”的东西。

1 个答案:

答案 0 :(得分:0)

可以建模如下:

static Tuple<string,string> Match(string question)
{
   //Do the matching and return the string,string tuple where first 
   //string is for output 1 and second for output 2.
}

static Tuple<List<string>,List<string>> GetOutput(List<string> questions)
{
    var r = questions.Select(q => Match(q));
    return new Tuple<List<string>,List<string>(r.Select(t => t.Item1).ToList(), r.Select(t =>  t.Item2).ToList());
}