SEARCH FOR A HEADER AND PUT THAT HEADER IN A LIST

Royce Roy

Total Post:149

Points:1043
Posted by  Royce Roy
C# 
C#
 993  View(s)
Ratings:
Rate this:

I have a textfile with blogposts, each blogpost is divided in a header and content shown below

    #Header

    A post        

    #Content

    My content goes here...

    #Header

    Another post

    #Content

    My content goes here...

Now I want to grab all the headers, the text that comes after #Header and before #Content and put that in a List<>. How can I do that?

This is what I have come up with so far:

var headers = new List<string>();

using (StreamReader reader = new StreamReader(Path))

        {

            string line;

            while ((line = reader.ReadLine()) != null)

            {

                headers.AddRange(line)

                    .SkipWhile(l => l != "#Header")

                    .Skip(1)

                    .TakeWhile(l => !l.StartsWith("#"))

                    .ToList();

            }

        }

Linq is preferred.

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: Search for a header and put that header in a list

    Hi Royce,

    You can use regex with LINQ

    string contents = File.ReadAllText("yourTextFile.txt");

    List<string> header=Regex.Matches(contents,@"#Header\s*(.*?)\s*#",RegexOptions.IgnoreCase | RegexOptions.Singleline)

    .Cast<Match>()

    .Select(x=>x.Groups[1].Value).ToList<string>();


Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!