---
title: "Search for a header and put that header in a list"  
description: "Search for a header and put that header in a list"  
author: "Royce Roy"  
published: 2014-03-29  
updated: 2014-03-29  
canonical: https://www.mindstick.com/forum/1999/search-for-a-header-and-put-that-header-in-a-list  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# Search for a header and put that header in a list

I have a textfile with blogposts, each blogpost is divided in a [header](https://www.mindstick.com/articles/336036/explain-about-html-header-body-and-footer-tags) and [content](https://www.mindstick.com/articles/13019/get-the-best-content-marketing-pricing-packages-for-your-brand) shown below

#Header

A [post](https://www.mindstick.com/articles/12829/aspects-that-make-australia-post-shipping-extension-a-useful-tool)

#Content

My content goes here...

#Header

Another post

#Content

My content goes here...

[Now](https://yourviews.mindstick.com/view/81402/it-s-liberals-vs-progressives-in-us-politics-now) I want to grab all the headers, the [text](https://www.mindstick.com/blog/301635/did-people-reinvent-texting-to-express-the-full-range-of-emotions) 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](https://www.mindstick.com/articles/1527/string-split-in-c-sharp)>();

using ([StreamReader](https://www.mindstick.com/forum/161576/explain-the-difference-between-streamreader-and-file-readalllines) reader = new StreamReader([Path](https://www.mindstick.com/articles/44333/4-expert-tips-on-how-to-pick-the-right-career-path)))

{

string line;

while ((line = reader.ReadLine()) != [null](https://www.mindstick.com/forum/33922/how-to-use-null-coalescing-operator-in-c-sharp))

{

headers.AddRange(line)

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

.Skip(1)

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

.ToList();

}

}

[Linq](https://www.mindstick.com/articles/12007/language-integrated-query-linq-queries) is preferred.

## Replies

### Reply by Pravesh Singh

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>();

\


---

Original Source: https://www.mindstick.com/forum/1999/search-for-a-header-and-put-that-header-in-a-list

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
