如何在C#中匹配模式并提取其中的一部分?

如何在C#中匹配模式并提取其中的一部分?,c#,C#,我有一个字符串,它总是以“?”结尾 有人能告诉我如何检查此文本是否包含: "Which of the following <any string of text here>?" “以下哪项?” 如果它包含这些内容,那么如何获取文本“abc def ghi”和“hello”您可以用来检查是否存在子字符串。 可以使用根据指定的子字符串拆分字符串 你也可以,如果你了解他们(我不知道…) 编辑: 此regex应匹配来自其他用户的所有输入: var text=“1-以下哪项是aa mmm

我有一个字符串,它总是以“?”结尾

有人能告诉我如何检查此文本是否包含:

"Which of the following <any string of text here>?"
“以下哪项?”
如果它包含这些内容,那么如何获取文本“abc def ghi”和“hello”

您可以用来检查是否存在子字符串。
可以使用根据指定的子字符串拆分字符串

你也可以,如果你了解他们(我不知道…)

编辑:regex应匹配来自其他用户的所有输入:

var text=“1-以下哪项是aa mmm zz?\n”+
“2-以下哪项是正确的?\n”+
“3-以下哪项可以是aa mmm zz?\n”+
“4-以下哪项是正确的?\n”+
“5-当aa mmm zz时会有什么结果?\n”+
“6-当aa mmm zz时会产生什么结果?\n”+
“7-哪些是aa mmm zz?\n”+
“8-关于aa mmm zz可以说些什么?\n”;
var regex=@“什么(可以说是什么?)(什么时候)会(是结果?)(?*?)\?(以下哪一项)?(可以是?)(是?)(是?:);
var matches=Regex.matches(文本,Regex);
foreach(匹配中的匹配)
{
控制台.WriteLine(“结果:+match.Groups[“结果”]);
}

如果您只想匹配整个输入字符串,并确保输入字符串以问题开头,请使用以下命令:

  static string ExtractQuestionObject(string question)
  {
     const string pattern = "Which of the following ";
     if (question.StartsWith(pattern, StringComparison.CurrentCultureIgnoreCase)
         && question.EndsWith("?"))
        return question.Substring(pattern.Length, question.Length - pattern.Length - 1);
     else
        return null;
  }
如果要在输入中的任何位置查找问题,请使用:

  static string FindQuestionObject(string text)
  {
     const string pattern = "Which of the following ";
     int questionStart = text.IndexOf(pattern);
     if (questionStart >= 0)
     {
        int questionMark = text.IndexOf('?', questionStart);
        if (questionMark > questionStart)
           return text.Substring(questionStart + pattern.Length,
                  questionMark - questionStart - pattern.Length);
     }
     return null;
  }
如果要使用正则表达式在一次调用中匹配所有字符串,请使用:

  static string[] FindQuestionObjects(string text)
  {
     System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex(
        @"Which of the following ([^\?]+)\?",
        System.Text.RegularExpressions.RegexOptions.IgnoreCase);
     System.Text.RegularExpressions.MatchCollection mc = re.Matches(text);
     string[] result = new string[mc.Count];
     for(int i = 0; i < result.Length; i++)
        result[i] = mc[i].Groups[1].Value;
     return result;
  }
静态字符串[]FindQuestionObjects(字符串文本)
{
System.Text.RegularExpressions.Regex re=新建System.Text.RegularExpressions.Regex(
@“以下哪项([^\?]+)\?”,
系统.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.MatchCollection mc=re.Matches(文本);
字符串[]结果=新字符串[mc.Count];
for(int i=0;i
介绍。为什么结果不是“可以是abc def ghi”和“可以是hello”。这不需要正则表达式,只需要一个简单的
字符串.IndexOf
。有些人在遇到问题时会想“我知道,我会使用正则表达式。”现在他们有两个问题。理想情况下,我正在寻找正则表达式解决方案。我现在所拥有的只是一个简单的例子,我希望它能让我开始。我有很多更先进的东西。我只是没有把它们放在这里,因为我认为我自己可以做到。非常感谢。我希望有人有一个RegEx的例子:-)@Christine-有人确实发布了一个RegEx解决方案,然后删除了它,因为他们可能认为它有点过头了。最好确保您的问题反映您的实际需求!谢谢,但是我有很多事情要做。我只希望有一行正则表达式,我可以尝试将其放入LINQ查询中。
var text = "1 - Which of the following is a aa mmm zz?\n" +
"2 - Which of the following is true?\n" +
"3 - Which of the following can be aa mmm zz?\n" +
"4 - Which of the following are correct?\n" +
"5 - What will be the result when aa mmm zz?\n" +
"6 - What will result when aa mmm zz?\n" +
"7 - Which are aa mmm zz?\n" +
"8 - What can be said about aa mmm zz?\n";

var regex = @"What (can be said about |will (be the )?result when )(?<result>.*?)\?|Which (of the following )?(can be|are|is|is a) (?<result>.*?)[?:]";
var matches = Regex.Matches(text, regex);

foreach (Match match in matches)
{
    Console.WriteLine("result: " + match.Groups["result"]);
}
  static string ExtractQuestionObject(string question)
  {
     const string pattern = "Which of the following ";
     if (question.StartsWith(pattern, StringComparison.CurrentCultureIgnoreCase)
         && question.EndsWith("?"))
        return question.Substring(pattern.Length, question.Length - pattern.Length - 1);
     else
        return null;
  }
  static string FindQuestionObject(string text)
  {
     const string pattern = "Which of the following ";
     int questionStart = text.IndexOf(pattern);
     if (questionStart >= 0)
     {
        int questionMark = text.IndexOf('?', questionStart);
        if (questionMark > questionStart)
           return text.Substring(questionStart + pattern.Length,
                  questionMark - questionStart - pattern.Length);
     }
     return null;
  }
  static string[] FindQuestionObjects(string text)
  {
     System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex(
        @"Which of the following ([^\?]+)\?",
        System.Text.RegularExpressions.RegexOptions.IgnoreCase);
     System.Text.RegularExpressions.MatchCollection mc = re.Matches(text);
     string[] result = new string[mc.Count];
     for(int i = 0; i < result.Length; i++)
        result[i] = mc[i].Groups[1].Value;
     return result;
  }