So, what I need to do in c# regex is basically split a string whenever I find a certain pattern, but ignore that pattern if it is surrounded by double quotes in the string.
Example:
string text = "abc , def , a\" , \"d , oioi";
string pattern = "[ \t]*,[ \t]*";
string[] result = Regex.Split(text, pattern, RegexOptions.ECMAScript);
Wanted result after split (3 splits, 4 strings):
{"abc",
"def",
"a\" , \"d",
"oioi"}
Actual result (4 splits, 5 strings):
{"abc",
"def",
"a\"",
"\"d",
"oioi"}
Another example:
string text = "a%2% 6y % \"ad%t6%&\" %(7y) %";
string pattern = "%";
string[] result = Regex.Split(text, pattern, RegexOptions.ECMAScript);
Wanted result after split (5 splits, 6 strings):
{"a",
"2",
" 6y ",
" \"ad%t6%&\" ",
"(7y) ",
""}
Actual result (7 splits, 8 strings):
{"a",
"2",
" 6y ",
"\"ad",
"t6",
"&\" ",
"(7y) ",
""}
A 3rd example, to exemplify a tricky split where only the first case should be ignored:
string text = "!!\"!!\"!!\"";
string pattern = "!!";
string[] result = Regex.Split(text, pattern, RegexOptions.ECMAScript);
Wanted result after split (2 splits, 3 strings):
{"",
"\"!!\"",
"\""}
Actual result (3 splits, 4 strings):
{"",
"\"",
"\"",
"\"",}
So, how do I move from pattern to a new pattern that achieves the desired result?
Sidenote: If you're going to mark someone's question as duplicate (and I have nothing against that), at least point them to the right answer, not to some random post (yes, I'm looking at you, Mr. Avinash Raj)...