Preface: This question is a derivative of this question.
Here is my code:
using System;
using System.Linq;
using System.Text.RegularExpressions;
c
Good comments through-out the thread (I would strongly suggest pursuing one of those options), I wont focus on them. Here's an alternative solution that uses Matches
from the Regex
pattern, skip
how many fields you have (columns) and then take
how many records you want.
I'm using a pattern like (\"(.*?)[^,]")
and explanation can be found here of what it means.
const string rawLine = "\"TeamName\",\"PlayerName\",\"Position\" \"Chargers\",\"Philip Rivers\",\"QB\" \"Colts\",\"Peyton Manning\",\"QB\" \"Patriots\",\"Tom Brady\",\"QB\"";
var matches = new Regex(@"(\""(.*?)[^,]"")").Matches(rawLine).Cast().ToList();
// loop through our matches
for(int i = 0; i < matches.Count; i++)
{
// join our records we need to output
string str = string.Join(",", matches.Skip(i * 3).Take(3));
if(!string.IsNullOrEmpty(str))
Console.WriteLine(str);
}
Console.WriteLine("Press [ENTER] to exit.");
Console.ReadLine();
Please note, there's no error checking at all, can be improved, but does produces the output you need. *Also make sure you import System.Linq
if not already there.
Output Test