Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I use <code>Regex.Split</code> only for the first example, with this regex</p> <pre><code>\s*,(?![^(]*\))\s* </code></pre> <p>that means split on a <code>,</code> (with optional whitespace before and ahead). The important part is this negative lookahead <code>(?![^(]*\))</code>, it fails if there is a closing round bracket ahead, but no opening before.</p> <p><strong>That means in the first step, I split on commas that are NOT within brackets.</strong></p> <p>For the second split I use a normal <code>String.Split</code> on round brackets an commas with the the <code>StringSplitOptions.RemoveEmptyEntries</code> to avoid having an empty entry at the end because of the closing bracket.</p> <p>For your examples, this gives you what you want:</p> <pre><code>String s1 = "name1(M, 06/06/1995), name2(M, 18/05/2000)"; String s2 = "name1(M, 26/01/2002)"; String s3 = "name1 (M), name 2(F)"; String s4 = "Nikesh, Nihar"; String[] input = { s1, s2, s3,s4 }; String[] Split1 = {}; List&lt;String[]&gt; Result = new List&lt;String[]&gt;(); foreach (String item in input) { Console.WriteLine("Splitting: " + item); Split1 = Split1.Concat(Regex.Split(item, @"\s*,(?![^(]*\))\s*")).ToArray(); } foreach (String item in Split1) { Console.WriteLine("Splitting: " + item); Result.Add(item.Split(new char[]{ '(', ')', ',' }, StringSplitOptions.RemoveEmptyEntries)); } foreach (String[] item in Result) { Console.WriteLine(string.Join(", ", item.Select(v =&gt; v.ToString()))); } Console.ReadLine(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload