【问题标题】:Regex pattern to get all lines text between two words [duplicate]正则表达式模式获取两个单词之间的所有行文本[重复]
【发布时间】:2015-11-08 13:46:18
【问题描述】:

我想创建一个正则表达式模式来获取两个单词之间的内容。

Start:
Apple
Cat
Ball
End:

我想获取 Start: 和 End: 之间的数据

我能够使用 C# 找到这些数据:

region 获取必填字段数据

    public static List<string> GetRequiredData(string[] lines, string StartPos, string EndPos)
    {
        List<String> RequiredField = new List<String>();


        bool hit = false;

        foreach (var line in lines)
        {
            if (line == EndPos)
            {
                hit = false;
            }

            else if (hit == true)
            {
                if (line != "\t"||line=="")
                {
                    RequiredField.Add(line);
                   
                }
            }

            else if (line == StartPos)
            {
                hit = true;
            }


        }
        return RequiredField;
    }
    #endregion Get Required Field Data

但我认为将正则表达式用于相同目的会很酷。 我尝试了 (?

我将非常感谢任何帮助。 谢谢,

【问题讨论】:

标签: regex


【解决方案1】:

我不是正则表达式专家,但我试了一下:

beginningword\n((.+\n)+)endingword

对于文本:

beginningword
first line of whatever
second line of whatever
third line of whatever
endingword

匹配到组 #1 的文本是:

first line of whatever
second line of whatever
third line of whatever

【讨论】:

  • 我尝试了上述模式,但没有成功。
  • (?s)(?
  • 我认为使用正则表达式是不可能的。
  • 没问题,我确实使用我的代码删除了空格。但我仍然认为可以使用正则表达式,不知道如何......我开始学习它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多