字符串形式  (1,3),(4,8),(8,100),(10,200)或者(1,4,8,10,20.56)或者混合的(1,3),4,(8,100),50,200
分割要求:   有()号的以每对括号为一组,没有括号的,以,分割为一组例如最后一组应该分割成strings[0]=(1,3)
strings[1]=4
strings[2]=(8,100)
strings[3]=50
strings[4]=200

解决方案 »

  1.   

    第一种,按楼主要求采用Split方法,但是会出现空字符串
    strings[0]=""
    strings[1]=(1,3) 
    strings[2]=""
    strings[3]=4 
    strings[4]=(8,100) 
    strings[5]=50 
    strings[6]=200            string str = "(1,3),(4,8),100,50,200";
                string[] strings = Regex.Split(str, @",|(\([^\)]*\)),",RegexOptions.IgnorePatternWhitespace);
    第二种,采用Matches方法,将捕获的组CopyTo一个数组,不会出现空字符串            MatchCollection mc = Regex.Matches(str, @"(\(\d+,\d+\))|(\d+)");
                object[] array = new object[mc.Count];
                mc.CopyTo(array, 0);
      

  2.   

    This one doesn't use regular expression :)
            static string[] MyGroupSplit(string str)
            {
                List<string> result = new List<string>();
                int parenthesis = 0;
                string current="";            foreach (char c in str)
                {
                    switch (c)
                    {
                        case '(':
                            current += c;
                            parenthesis++;
                            break;
                        case ',':
                            if (parenthesis == 0) { if(current != "") result.Add(current); current = ""; }
                            else current += c;
                            break;
                        case ')':
                            if (--parenthesis == 0) { result.Add(current + ")"); current = ""; }
                            break;
                        default:
                            current += c;
                            break;
                    }
                }
                return result.ToArray();
            }
      

  3.   

                MatchCollection mc = Regex.Matches(textBox1.Text, @"\([^\)]*\)|(?<=^|,)[^,]*(?=,|$)");
                foreach (Match m in mc)
                    textBox2.AppendText(m.Value+Environment.NewLine);
    输入
    (1,3),4,(8,100),50,200 
    输出
    (1,3)
    4
    (8,100)
    50
    200 不过也许你真正想要的是这个:
    MatchCollection mc = Regex.Matches(textBox1.Text, @"(?<=^\(|,\()[^)]*(?=\))|(?<=^|,)[^\(\),]*(?=,|$)");
    输出:
    1,3
    4
    8,100
    50
    200