我需要使用C#中的多个模式进行某种模式匹配作为输入 [英] I need to do some pattern matching with more than one pattern as input in C#

查看:103
本文介绍了我需要使用C#中的多个模式进行某种模式匹配作为输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要进行一些模式匹配以处理问题列表并提出 一些声明将列在解决方案列表的首位.我需要匹配的每个问题 第一句以?"结尾.例如,如果具有以下字符串:

I need to do some pattern matching to process a list of questions and come up with some statements that will head the list of solutions. Every question that I need to match has a first sentence that ends in a "?". For example if have the following string:

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

然后,我需要查看它是否与某些预定义模式匹配,如果匹配,则需要填充两个变量.在此示例中,上面的字符串需要产生以下内容:

then I need to see if this matches some predefined patterns and if it does then I need to populate two variables. In this example the string above needs to result in the following:

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

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

Where the text aa ... zzz in the source appears in the destination.

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

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

I am thinking that I should store the input pattern and output patterns in a reference data class and then maybe some combination of link and pattern matching.

有人有任何建议吗?即使我能看到如何进行一场比赛也很好. 如果我有一个例子,那么我可以编写更多代码并以此作为工作.

Does anyone have any suggestions. Even if I could see how to do one match that would be good. If I have an example for one then I can code in more and work from that.

什么样的模式? -图案是文字.因此,例如,我要查看我的输入字符串是否以以下内容可以是"开头,并以?"结尾.

What kind of patterns? - The patterns are the text. So for example I am looking to see if my input string starts with "Which of the following can be" and ends with a "?".

这些问题来自哪里? -一组有限的问题.如果我找不到匹配项,则将在输出中输入"True"或"False"之类的内容.

Where are these questions coming from? - A limited set of questions. If I cannot get a match then I will put in something like "True" or "False" in the outputs.

推荐答案

可以对其进行建模,如下所示:

It can be modeled something like below:

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());
}

这篇关于我需要使用C#中的多个模式进行某种模式匹配作为输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆