读书人

怎么将一组字符串用正则分离出来?多谢

发布时间: 2012-07-26 12:01:08 作者: rapoo

如何将一组字符串用正则分离出来?谢谢。

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               ptfont-color       #           111111width            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>[^:|]+……

读书人网 >C#

热点推荐