-3

I need to separate the following string by comma and get the chunks down below. What's the most elegant solution in C#? String.Split() detects also internal commas, of course.

"'_82X5_00_11 (2,RAL 7035)', '_82X5_00_11 (2,RAL 7035)', #349, #1 "

The result should be:

'_82X5_00_11 (2,RAL 7035)'
'_82X5_00_11 (2,RAL 7035)'
#349
#1

Thanks.

abenci
  • 8,422
  • 19
  • 69
  • 134

1 Answers1

0

Try this:

string aux = "'_82X5_00_11 (2,RAL 7035)', '_82X5_00_11 (2,RAL 7035)', #349, #1 ";
var result = Regex.Split(aux, ("(?!\\B'[^']*),(?![^']*'\\B)"));

EDIT

This was not getting the " '', '' " because '' doesn't contain anything.

Solution:

string pattern = "('[^']+'|[^',^\\s]+|['']+)";
string str1 = "'_82X5_00_11 (2,RAL 7035)', '_82X5_00_11 (2,RAL 7035)', #349, #1 ";
string str2 = "'', '', #344, #334";
var result1 = Regex.Matches(str1, pattern).Cast<Match>().Select(x => x.Value).ToArray();
var result2 = Regex.Matches(str2, pattern).Cast<Match>().Select(x => x.Value).ToArray();
Leandro Soares
  • 2,902
  • 2
  • 27
  • 39