日期:2014-05-18  浏览次数:20840 次

求个分割字符串的正则表达式
需要分割一个字符串,用逗号分割,单引号中的作为一个整体,但里面可能包含逗号,所以不能直接用split,我写了个正则表达式,但是分割的单引号旁边会有两个空行,求一个正确的正则表达式

string ss = "'64031,TJ,Han',100401,54321,IL,CA,1075,10000,30,4000";
  System.Text.RegularExpressions.Regex reg3 = new System.Text.RegularExpressions.Regex("['](.*)[']|[,]");
  ///开始分割字符串
  string[] name = reg3.Split(ss);


------解决方案--------------------
C# code
            string str = "'64031,TJ,Han',100401,54321,IL,CA,1075,10000,30,4000";
            string[] result = Regex.Split(str, @"(?<!'[^,]+),(?![^,]+')");
            foreach(string s in result)
                Console.WriteLine(s);

------解决方案--------------------
探讨

C# code
string str = "'64031,TJ,Han',100401,54321,IL,CA,1075,10000,30,4000";
string[] result = Regex.Split(str, @"(?<!'[^,]+),(?![^,]+')");
foreach(string s in re……