1

I want to create a regex pattern to get stuff between two words.

Start:
Apple
Cat
Ball
End:

I want to get the data between Start: and End:

I was able to find this data using C#:

region Get Required Field Data

    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

But i think using regex for the same purpose will be cool. I tried (?<=Start:)(.*)(?=End:) but it is not working.Plus i also want to remove any line in between with no text.

I will really appreciate any help. Thank You,

Community
  • 1
  • 1

1 Answers1

6

I'm not an expert in regex, but i gave it a try:

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

For a text:

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

Text that is matched to be group #1 is:

first line of whatever
second line of whatever
third line of whatever
piezol
  • 915
  • 6
  • 24
  • I tried above pattern but it didn't worked. –  Aug 16 '15 at 04:59
  • (?s)(?<=beginningword).*?(?=endingword) worked for me. But it doesn't remove extra spaces. For an example: beginningword first line of whatever second line of whatever third line of whatever endingword It will also take the blank line.I was able to remove that line using methods String class in C#.Is it possible to remove these blank line using regex? –  Aug 16 '15 at 05:06
  • I think it's not possible with regex. – piezol Aug 18 '15 at 22:54
  • no problem i did remove the spaces using my code.But i still think it is possible using regex,Not sure how....I started learning it –  Aug 25 '15 at 10:28