Split string by ',' into array, except ',' inside ()

I have a line like

"11,Standard(db=S,api=Standard),UI,1(db=1,api=STANDARD),Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36,1010,9999,1000,9998.0,,1" 

I would like to split it into char ', but I don't like to include', 'in' () '. Please help how to break such a line in C #. Ignore char ',' inside ().

The output should look like this:

  array of string = [ "11", "Standard(db=S,api=Standard)", "UI", "1(db=1,api=STANDARD)", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36", "1010" 

and etc.

+1
source share
6 answers

If the pattern is always , you can use the regular expression to separate the delimiters (commas) than to match the tokens. Identifying commas that are separators is relatively simple:

 ",(?=[^\)]*(?:\(|$))" 

Demo: https://dotnetfiddle.net/GgboVn

Explanation: http://regexr.com/3bgfj

+3
source

here is the parser

 public static IEnumerable<string> Parse(string Input) { int depth = 0; StringBuilder Line = new StringBuilder(); foreach (char item in Input) { if (depth == 0 && item == ',') { yield return Line.ToString(); Line = new StringBuilder(); } else { Line.Append(item); if (item == '(') { depth++; } if (item == ')') { depth--; } } } if (Line.Length > 0) yield return Line.ToString(); } 

Using:

 var result = Parse(input); 
+2
source

Here is a simple parser. Not bullet proof and depending on your needs some changes may be required:

 static void Main(string[] args) { var toSplit = "11,Standard(db=S,api=Standard),UI,1(db=1,api=STANDARD),Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36,1010,9999,1000,9998.0,,1"; var list = new List<string>(); var isInside = 0; var currentPart = string.Empty; for (int i = 0; i < toSplit.Length; i++) { var chr = toSplit[i]; switch (chr) { case ',': if(isInside == 0) { list.Add(currentPart); currentPart = string.Empty; } break; case '(': isInside++; currentPart += chr; break; case ')': isInside--; currentPart += chr; break; default: currentPart += chr; break; } } foreach (var part in list) { Console.WriteLine(part); } Console.ReadLine(); } 
+1
source

This should do what you want. He will split your entry into the delimitter outsinde of any Parenthesis

 static IEnumerable<string> SplitStringMyWay(string text, char delimitter) { string[] internalItems = text.Split(delimitter); List<string> theItems = new List<string>(); string newItem = string.Empty; int openParenthesis = 0; foreach (string item in internalItems) { if (openParenthesis != 0) newItem += ","; newItem += item; openParenthesis += GetCharCount('(', item); openParenthesis -= GetCharCount(')', item); if (openParenthesis == 0) { theItems.Add(newItem); newItem = string.Empty; } } return theItems; } static int GetCharCount(char value, string text) { int count = 0; foreach (char character in text) { if (character == value) { count++; } } return count; } 

The code is not test and may contain errors. If you find, feel free to edit my answer

0
source

IMO the easiest way is to first split your string with ',' and then combine the strings with the characters '(' and ')' together:

 string value = @"11,Standard(db=S,api=Standard),UI,1(db=1,api=STANDARD),Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36,1010,9999,1000,9998.0,,1"; List<string> list = new List<string>(); string[] temp = value.Split(','); for (var i = 0; i < value.Split(',').Length; i++) { if (temp[i].Contains('(')) continue; if (temp[i].Contains(')')) { list.Add(temp[i - 1] + temp[i]); continue; } list.Add(temp[i]); } foreach (var item in list) Console.WriteLine(item); 

Output:

 11 Standard(db=S,api=Standard) UI 1(db=1,api=STANDARD) 

PS Of course, you can also write your parser:

  static string[] MySplit(string dirty, char delimiter = ',', string ignoreInside = "()") { StringBuilder sb = new StringBuilder(); bool sectionStarted = false; List<string> result = new List<string>(); for (int i = 0; i < dirty.Length; i++) { if (!sectionStarted && dirty[i] == delimiter) { result.Add(sb.ToString()); sb.Clear(); continue; } if (ignoreInside.Contains(dirty[i])) sectionStarted = dirty[i] == ignoreInside[0]; sb.Append(dirty[i]); } return result.ToArray(); } 

Usage: var result = MySplit(value);

The conclusion is the same as in the example above.

0
source

It works

  var values = "11,Standard(db=S,api=Standard),UI,1(db=1,api=STANDARD),Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36,1010,9999,1000,9998.0,,1"; var splitList = GetSplitList(values); public static List<string> GetSplitList(string values) { var splitList = new List<string>(); var retValue = string.Empty; foreach (var value in values.Split(',')) { if (!string.IsNullOrEmpty(retValue) && !value.Contains(")")) { retValue += string.Format("{0},", value); continue; } if (value.Contains("(")) { retValue += string.Format("{0},", value); continue; } if (value.Contains(")")) { retValue += value; splitList.Add(retValue); retValue = string.Empty; continue; } splitList.Add(value); } return splitList; } 
0
source

All Articles