RegEx in c# for a piped (|) pattern string

I have an multiline input string of following format

ID | Col1 | Col2 | Col3 | Col4 | Col5 | Col6 | Col7 | Col8

The data might run into thousands of rows Data Type for Col1-8 could be alphanumeric or alpha only which may differ from row to row.

I need to do following:

  1. Extract Col2 & Col3 out (Thinking of using Regex here instead of usual string splitting since later would be slower in my case?)

  2. I want the rest of the data also in following format also. ID | Col1 | Col4 | Col5 | Col6 | Col7 | Col8 (This I can achieve using string.Replace once I have the data in point 1)

Please note that I do not want to loose data of Col2 & Col3 while achieving solution for point 2 and hence the first point is also important to me.

I tried creating RegEx for point 1 as (|){2,4} which I understand now is completely wrong and curently I have no idea how to proceed on this using Regex.

I would be thankful for any help/pointers on how to go about creating a regex for same.


Is there a reason you can't do it this way?

var str = "ID | Col1 | Col2 | Col3 | Col4 | Col5 | Col6 | Col7 | Col8";
var strA = str.Split(" | ".ToArray(), StringSplitOptions.RemoveEmptyEntries).ToList();
var strExtract = new List<string> { strA[2], strA[4] };
Console.WriteLine(string.Join(" | ", strA.ToArray()));

var cols = input.Split(new[] {'|'}, StringSplitOptions.RemoveEmptyEntries)
            .Select(s => s.Trim())

var extractedcols = cols.Skip(2).Take(2);
var output = string.Join(" | ", cols.Except(extractedcols));

Use .Split'|', 5) to get the first 4 elements and the rest of the sting as an array.

Need Your Help

Words Counter using Dictionary

java arrays dictionary

For homework I have to implement a Words Counter using a Dictionary.I have an inner class named Couple

EXTJS 4.2 - combobox resets when match is not found

extjs extjs4.2 extjs-mvc

The typeahead works fine if the words match, however if an incorrect character is entered, thus no match then all the text entered vanishes.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.