adityap
adityap

Reputation: 351

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.

Upvotes: 0

Views: 139

Answers (3)

cuongle
cuongle

Reputation: 75306

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

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

Upvotes: 1

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

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] };
strA.RemoveAt(2);
strA.RemoveAt(3);
Console.WriteLine(string.Join(" | ", strA.ToArray()));

Upvotes: 0

Huusom
Huusom

Reputation: 5912

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

Upvotes: 0

Related Questions