I've seen a lot of Regex answers that get very close to what I need, but it's not quite there. The problem is that I have a string that I need to split on a character (e.g.: space or '=') but I want to ignore anything that is inside of quotes (even quotes inside of quotes).
The closest I've been able to get is this:
" (?=(?:[^"]*"[^"]*")*[^"]*$)"
Which works great, with two caveats: poorly timed spaces in the quotes trigger a bad split, and it reads backwards. The first problem I don't really care about, there's not much I can do and I can work around it. But the second is critical.
The case is that sometimes the string I'm regexing may be accidentally missing a quote on the end. This doesn't really bother my system, but the regex above goes backward, so it breaks everything:
string test = "foo bar \"foo bar\" foobar \"foo"
var result = Regex.Split(test, " (?=(?:[^"]*"[^"]*")*[^"]*$)");
This will make:
foo bar "foo
bar" foobar "foo
Because it starts at the end and runs the filter backwards. I need the result to be:
foo
bar
"foo bar"
foobar
"foo
I know the $ is responsible for the start at the end thing, but I can't for the life of me figure out how to reverse it. Thoughts?