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

如何将一组字符串用正则分离出来?谢谢。
C# code
string str = "font-size:12pt|font-family:微软雅黑|font-weight:bold|font-color:#111111|width:640px|height:120px|bgcolor:#ffffff";


结果是:
SQL code

font-size        null        12               pt
font-color       #           111111
width            null        120              px
--……



谢谢。

------解决方案--------------------
C# code

  string tempStr = "font-size:12pt|font-family:微软雅黑|font-weight:bold|font-color:#111111|width:640px|height:120px|bgcolor:#ffffff";  
                foreach (Match m in Regex.Matches(tempStr, pattern))
                {
                    //循环输出
                    string value = m.Value;
                    string PA = m.Groups[1].Value;//font-size
                    string PB = m.Groups[2].Value;//null
                    string PC = m.Groups[3].Value;//12
                    string PD = m.Groups[4].Value;//pt
                }

------解决方案--------------------
C# code
 string pattern = @"(?i)([^:|]+):([^|\d]*)(\d+)([^|\s]*)";

------解决方案--------------------
C# code
            string str = "font-size:12pt|font-family:微软雅黑|font-weight:bold|font-color:#111111|width:640px|height:120px|bgcolor:#ffffff";
            Regex reg = new Regex(@"(?is)(?<PA>[^:|]+):(?<PB>#)?(?<PC>\d+|[^\d|]+)(?<PD>p[xt])?");
            foreach (Match m in reg.Matches(str))
                Console.WriteLine("{0}=={1,1}=={2}=={3,2}==", m.Groups["PA"].Value, m.Groups["PB"].Value, m.Groups["PC"].Value, m.Groups["PD"].Value);
/*
font-size== ==12==pt==
font-family== ==微软雅黑==  ==
font-weight== ==bold==  ==
font-color==#==111111==  ==
width== ==640==px==
height== ==120==px==
bgcolor==#==ffffff==  ==

*/

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

C# code
string str = "font-size:12pt|font-family:微软雅黑|font-weight:bold|font-color:#111111|width:640px|height:120px|bgcolor:#ffffff";
Regex reg = new Regex(@"(?is)(?<PA>[^:|]+……