Preface: This question is a derivative of this question.
Here is my code:
using System;
using System.Linq;
using System.Text.RegularExpressions;
c
As Amy has already mentioned, your string seems to be something like CSV. If it is really a valid CSV - use special libraries.
If CSVHelper isn't applicable in this case and you really need regex, try something like this one:
(?<=(?:^| ))(.*?)(?=(?: \")|$)
I haven't coded for C#, so regex may need some edits due to c# specific.
Edit. Code example.
using System;
using System.Linq;
using System.Text.RegularExpressions;
class MainClass {
public static void Main (string[] args) {
const string rawLine = "\"TeamName\",\"PlayerName\",\"Position\" \"Chargers\",\"Philip Rivers\",\"QB\" \"Colts\",\"Peyton Manning\",\"QB\" \"Patriots\",\"Tom Brady\",\"QB\"";
//var parsedLines = Regex.Split(rawLine, "(?<=(?:^| ))(.*?)(?=(?: \")|$)");
var parsedLines = Regex.Split(rawLine, "(?<=^)(.*?)(?=(?: \")|$)|(?<= )(.*?)(?=(?: \")|$)");
parsedLines.ToList().ForEach(Console.WriteLine);
Console.WriteLine("Press [ENTER] to exit.");
Console.ReadLine();
}
}
This code with "dirty" fix for assertion error. However, i can't reproduce it with onlinetool :) Original regex commented in this example.
I hope, this will help you. But i must say again if you working with csv - it is better to use special tools, not regex :)