已知有若干条记录(都是字符串),形式如下:型号=Pentium D 805(盒装);接口类型=LGA 775;核心类型=SmithField;生产工艺=0.09um;核心电压=1.3V;型号=Pentium D 805(盒装);适用类型=台式机;接口类型=LGA 775;核心类型=SmithField;生产工艺=0.09um;型号=Pentium D 805(盒装);适用类型=台式机;生产工艺=0.09um;核心电压=1.3V;其共有的属性和为:型号,适用类型,接口类型,核心类型,生产工艺,核心电压
每条记录可能只记录其中某几条属性的值。需将每条记录中分离出各个属性的值,如记录中没有该属性的值则其值为空。例如:
型号=Pentium D 805(盒装);适用类型=台式机;生产工艺=0.09um;核心电压=1.3V;
分解出结果应该是
型号  Pentium D 805(盒装)
适用类型  台式机
接口类型
核心类型
生产工艺  0.09um
核心电压  1.3V不知道怎么解决好。不知道用正则可以不可以做出来

解决方案 »

  1.   

    看你的例子,这些属性可能有也可能没有,如果有顺序还不确定是吧正则可以处理,但是效率不高,而且看你的结果要求,觉得没必要用正则这些记录应该是单条处理的是吧,那就先用“;”分割,Split到一个数组,然后用StartsWith判断是以哪个属性开头的,分别赋给不同的字符串就可以了,这样做会比正则的效率高
      

  2.   

    正则没弄出来。用的不是正则请参考
     string str = "型号=Pentium D 805(盒装);适用类型=台式机;生产工艺=0.09um;核心电压=1.3V;";
                string[] arra = str.Split(new char[]{';'});
                string[] arra1;
                for (int i = 0; i < arra.Length; i++)
                {
                    string strResult = "";
                    arra1 = arra[i].Split(new char[] { '=', ' ', ' ' });
                    foreach (string s in arra1)
                    {
                        strResult += s+" ";
                    }
                    Console.WriteLine(strResult);
                }
      

  3.   

    随便写了一下,这样比正则高效且灵活,正则要写,因为顺序不定,效率会很受影响
    string yourStr = ............;string type = "型号  ";
    string aType = "适用类型  ";
    string iType = "接口类型  ";
    string cType = "核心类型  ";
    string pTechnics = "生产工艺  ";
    string cVoltage = "核心电压  ";string[] arr = yourStr.Split(';');
    foreach (string s in arr)
    {
        if (s.IndexOf("=") > -1)
        {
            string strHead = s.Substring(0, s.IndexOf("="));
            string content = s.Substring(s.IndexOf("=") + 1);
            switch (strHead)
            {
                case "型号":
                    type += content;
                    break;
                case "适用类型":
                    aType += content;
                    break;
                case "接口类型":
                    iType += content;
                    break;
                case "核心类型":
                    cType += content;
                    break;
                case "生产工艺":
                    pTechnics += content;
                    break;
                case "核心电压":
                    cVoltage += content;
                    break;
            }
        }
    }
      

  4.   

    To:jetxia  &  ojekleen
    我尝试用正则去分解,但是一直没搞定,如果可以,请大家帮忙想想
      

  5.   

    谢谢lxcnn提供的思路。其实顺序应该是固定的,只是记录的属性不完全,会缺失。
      

  6.   

    可能是我把问题想复杂了,这样看是不是你要的结果string yourStr = .................;   //待处理记录string[] attribute = new string[] { "型号", "适用类型", "接口类型", "核心类型", "生产工艺", "核心电压"};  //产品属性数组,根据产品的不同,可动态替换Hashtable ht = new Hashtable();
    foreach (string s in attribute)
    {
        ht.Add(s, "");
    }string[] arr = yourStr.Split(';');foreach (string s in arr)
    {
        if (s.IndexOf("=") > -1)
        {
            string strHead = s.Substring(0, s.IndexOf("="));
            string content = s.Substring(s.IndexOf("=") + 1);
            if (ht.Contains(strHead))
            {
                ht[strHead] = content;
            }
        }
    }foreach (DictionaryEntry de in ht)
    {
        richTextBox2.Text += de.Key.ToString() + "  " + de.Value.ToString() + "\n";
    }
    只是一个大概的思路,如果满足要求,实现方法自己优化一下
      

  7.   

    谢谢lxcnn的思路。
    这个问题基本解决了,我是通过拼凑正则表达式的方法来解决的,我觉得我的方法很蠢,不知道还有没有更好的方法。
    上面的需求是个简化了的模型,实际情况和我实现的主要代码如下:
    using System;
    using System.Collections;
    using System.Text;
    using System.Text.RegularExpressions;public class MyClass
    {
    public static void Main()
    {
    string strregex="";
    string regextemp="({0}.*?#ffffff'>(?<{0}>.*?)</).*?";
    string source=@"<TR>
                                      <TD CLASS=btd WIDTH=198 BGCOLOR='#FCFCFC'><B>  型号</B></TD>
                                      <TD ALIGN=left WIDTH=316 BGCOLOR='#ffffff'> Pentium D 805(盒装)</TD>
                                    </TR>
                         
                                    <TR>
                                      <TD CLASS=btd WIDTH=198 BGCOLOR='#FCFCFC'><B>  <a href='http://dict.pconline.com.cn/dic/sort.jsp?kindId=-1&dicId=531' target='_blank'>接口类型</a></B></TD>
                                      <TD ALIGN=left WIDTH=316 BGCOLOR='#ffffff'> LGA 775</TD>
                                    </TR>";
    string stratt="型号,适用类型,接口类型,核心类型";
    string[] fullattlist=stratt.Split(',');
    Hashtable ht=new Hashtable();
    ArrayList realattlist=new ArrayList();
    string temp;
    for(int i=0;i<fullattlist.Length;i++)
    {
    if(source.IndexOf(fullattlist[i])>0)
    {
    ht.Add(i,fullattlist[i]);
    temp=string.Format(regextemp,fullattlist[i]);
    realattlist.Add(fullattlist[i]);
    strregex+=temp;
    }
    }
    Console.WriteLine(strregex);
    MatchCollection MatchList=RegexHelper.DoRegex(source,strregex);
    for (int i = 0; i < MatchList.Count; i++)
            {
    for(int j=0;j<fullattlist.Length;j++)
    {
    string att=fullattlist[j].ToString();
    Console.Write(att);
    Console.Write(":");
    Console.WriteLine(MatchList[i].Groups[att].Value);
    }
    }
    }

    }
    输出结果:
    型号: Pentium D 805(盒装)
    适用类型:
    接口类型: LGA 775
    核心类型:
      

  8.   

    哦,是处理html源文件,那包含“适用类型”的<tr>应该也存在吧,只是对应它的<td>是空的而已,是不是这样,如果是,那确实没必要这样麻烦的,给个完整的<table>的例子看下另外你要的结果,是只要输出为
    型号: Pentium D 805(盒装)
    适用类型:
    接口类型: LGA 775
    核心类型:
    这种形式就可以了,也就是可以看做一个多行字符串,还是说需要一行一行的,分做多行来输出,或者是分行写入数据库之类的
      

  9.   

    实际的结果是需要将HTML中的属性和值,分离出来一一对应存储在某个实体对象中,再存储到数据库中。
      

  10.   

    既然最后还是要存数据库的,那么产品以及产品对应的属性应该是已知的但如果要写成一个通用的方法,需要你的数据源都符合一定的规律,比如说产品这个<table>里每一个<tr>都只有两个<td>,分别是属性和对应的值,如果是这样,那还是可以写出一个通用方法的如果有这个需要,给出一个完整的<table>实例吧
      

  11.   

    <TABLE CELLSPACING=1 CELLPADDING=3 WIDTH="98%" ALIGN=center BORDER=0 BGCOLOR="#cccccc">
                        
                                    <TR bgcolor="#eeeeee">
                                      <TD  class=article1><FONT color="#ff6303"><B> 主要参数</B></FONT></TD>
      <TD  align="right" style="padding-right:10px"><img src="http://www.pconline.com.cn/product/images/200606_dian.gif" width="3" height="5" align="absmiddle"> <a href="http://pdlib.pconline.com.cn/product/guest/cavil4Guest.jsp?productId=141432" target="_blank"><font color="FF8000"><b>我要挑错</b></font></a></TD>
                                    </TR>
                                    
                                    <TR>
                                      <TD CLASS=btd WIDTH=198 BGCOLOR="#FCFCFC"><B>  型号</B></TD>
                                      <TD ALIGN=left WIDTH=316 BGCOLOR="#ffffff"> Pentium D 805(盒装)</TD>
                                    </TR>
                         
                                    <TR>
                                      <TD CLASS=btd WIDTH=198 BGCOLOR="#FCFCFC"><B>  适用类型</B></TD>
                                      <TD ALIGN=left WIDTH=316 BGCOLOR="#ffffff"> 台式机</TD>
                                    </TR>
                         
                                    <TR>
                                      <TD CLASS=btd WIDTH=198 BGCOLOR="#FCFCFC"><B>  <a href="http://dict.pconline.com.cn/dic/sort.jsp?kindId=-1&dicId=531" target="_blank">接口类型</a></B></TD>
                                      <TD ALIGN=left WIDTH=316 BGCOLOR="#ffffff"> LGA 775</TD>
                                    </TR>
                         
                                    <TR>
                                      <TD CLASS=btd WIDTH=198 BGCOLOR="#FCFCFC"><B>  核心类型</B></TD>
                                      <TD ALIGN=left WIDTH=316 BGCOLOR="#ffffff"> SmithField</TD>
                                    </TR>
                         
                                    <TR>
                                      <TD CLASS=btd WIDTH=198 BGCOLOR="#FCFCFC"><B>  生产工艺</B></TD>
                                      <TD ALIGN=left WIDTH=316 BGCOLOR="#ffffff"> 0.09um</TD>
                                    </TR>
                         
                                    <TR>
                                      <TD CLASS=btd WIDTH=198 BGCOLOR="#FCFCFC"><B>  <a href="http://dict.pconline.com.cn/dic/sort.jsp?kindId=-1&dicId=3314" target="_blank">核心电压</a></B></TD>
                                      <TD ALIGN=left WIDTH=316 BGCOLOR="#ffffff"> 1.3V</TD>
                                    </TR>
                         
                                    <TR>
                                      <TD CLASS=btd WIDTH=198 BGCOLOR="#FCFCFC"><B>  <a href="http://dict.pconline.com.cn/dic/sort.jsp?kindId=-1&dicId=3318" target="_blank">主频</a></B></TD>
                                      <TD ALIGN=left WIDTH=316 BGCOLOR="#ffffff"> 2.66</TD>
                                    </TR>
                         
                                    <TR>
                                      <TD CLASS=btd WIDTH=198 BGCOLOR="#FCFCFC"><B>  <a href="http://dict.pconline.com.cn/dic/sort.jsp?kindId=-1&dicId=3317" target="_blank">外频</a></B></TD>
                                      <TD ALIGN=left WIDTH=316 BGCOLOR="#ffffff"> 外频 133MHz</TD>
                                    </TR>
                         
                                    <TR>
                                      <TD CLASS=btd WIDTH=198 BGCOLOR="#FCFCFC"><B>  <a href="http://dict.pconline.com.cn/dic/sort.jsp?kindId=-1&dicId=532" target="_blank">倍频</a></B></TD>
                                      <TD ALIGN=left WIDTH=316 BGCOLOR="#ffffff"> 20X</TD>
                                    </TR>
                         
                                    <TR>
                                      <TD CLASS=btd WIDTH=198 BGCOLOR="#FCFCFC"><B>  <a href="http://dict.pconline.com.cn/dic/sort.jsp?kindId=-1&dicId=3311" target="_blank">一级缓存</a></B></TD>
                                      <TD ALIGN=left WIDTH=316 BGCOLOR="#ffffff"> L1 32K</TD>
                                    </TR>
                         
                                    <TR>
                                      <TD CLASS=btd WIDTH=198 BGCOLOR="#FCFCFC"><B>  <a href="http://dict.pconline.com.cn/dic/sort.jsp?kindId=-1&dicId=3312" target="_blank">二级缓存</a></B></TD>
                                      <TD ALIGN=left WIDTH=316 BGCOLOR="#ffffff"> L2 2*1024K</TD>
                                    </TR>
                         
                                    <TR>
                                      <TD CLASS=btd WIDTH=198 BGCOLOR="#FCFCFC"><B>  <a href="http://dict.pconline.com.cn/dic/sort.jsp?kindId=-1&dicId=3243" target="_blank">前端总线频率</a></B></TD>
                                      <TD ALIGN=left WIDTH=316 BGCOLOR="#ffffff"> 533MHz</TD>
                                    </TR>
                          
                                    <TR bgcolor="#eeeeee">
                                      <TD  class=article1><FONT color="#ff6303"><B> 功能参数</B></FONT></TD>
      <TD  align="right" style="padding-right:10px"><img src="http://www.pconline.com.cn/product/images/200606_dian.gif" width="3" height="5" align="absmiddle"> <a href="http://pdlib.pconline.com.cn/product/guest/cavil4Guest.jsp?productId=141432" target="_blank"><font color="FF8000"><b>我要挑错</b></font></a></TD>
                                    </TR>
                                    
                                    <TR>
                                      <TD CLASS=btd WIDTH=198 BGCOLOR="#FCFCFC"><B>  <a href="http://dict.pconline.com.cn/dic/sort.jsp?kindId=-1&dicId=3565" target="_blank">64位处理器</a></B></TD>
                                      <TD ALIGN=left WIDTH=316 BGCOLOR="#ffffff"> 是</TD>
                                    </TR>
                         
                                    <TR>
                                      <TD CLASS=btd WIDTH=198 BGCOLOR="#FCFCFC"><B>  <a href="http://dict.pconline.com.cn/dic/sort.jsp?kindId=-1&dicId=3566" target="_blank">核心数量</a></B></TD>
                                      <TD ALIGN=left WIDTH=316 BGCOLOR="#ffffff"> 双核</TD>
                                    </TR>
                          
                                    <TR bgcolor="#eeeeee">
                                      <TD  class=article1><FONT color="#ff6303"><B> 其它参数</B></FONT></TD>
      <TD  align="right" style="padding-right:10px"><img src="http://www.pconline.com.cn/product/images/200606_dian.gif" width="3" height="5" align="absmiddle"> <a href="http://pdlib.pconline.com.cn/product/guest/cavil4Guest.jsp?productId=141432" target="_blank"><font color="FF8000"><b>我要挑错</b></font></a></TD>
                                    </TR>
                                    
                                    <TR>
                                      <TD CLASS=btd WIDTH=198 BGCOLOR="#FCFCFC"><B>  包装</B></TD>
                                      <TD ALIGN=left WIDTH=316 BGCOLOR="#ffffff"> 盒装</TD>
                                    </TR>
                         
                                    <TR>
                                      <TD CLASS=btd WIDTH=198 BGCOLOR="#FCFCFC"><B>  保修时间、方式</B></TD>
                                      <TD ALIGN=left WIDTH=316 BGCOLOR="#ffffff"> 三年保修</TD>
                                    </TR>
                           
                    </TABLE>
      

  12.   

    MatchCollection mc0 = Regex.Matches(yourStr, @"(?<=<TD\s*class=article1>[\s\S]*?</tr>)[\s\S]*?(?=(<TD\s*class=article1>|</table>))", RegexOptions.IgnoreCase);
    foreach (Match m0 in mc0)
    {    Hashtable ht = new Hashtable();    MatchCollection mc = Regex.Matches(m0.Value, @"<tr>\s*<td[^>]*>(?<key>[\s\S]*?)</td>\s*<td[^>]*>(?<value>[\s\S]*?)</td>\s*</tr>", RegexOptions.IgnoreCase);
        foreach (Match m in mc)
        {
            string key = Regex.Replace(m.Groups["key"].Value, @"<[^>]*>", "").Trim();
            string value = Regex.Replace(m.Groups["value"].Value, @"<[^>]*>", "").Trim();
            ht.Add(key, value);
        }    foreach (DictionaryEntry de in ht)
        {
            richTextBox2.Text += de.Key.ToString() + "  " + de.Value.ToString() + "\n";
        }    richTextBox2.Text += "\n\n\n";
    }上面那个外层的foreach是我后加的,是为了根据<TD  class=article1>分三部分取出其中的内容,如果不区分,只要是这个<table>里的,就对应取出的话,用下面的即可Hashtable ht = new Hashtable();MatchCollection mc = Regex.Matches(yourStr, @"<tr>\s*<td[^>]*>(?<key>[\s\S]*?)</td>\s*<td[^>]*>(?<value>[\s\S]*?)</td>\s*</tr>", RegexOptions.IgnoreCase);
    foreach (Match m in mc)
    {
        string key = Regex.Replace(m.Groups["key"].Value, @"<[^>]*>", "").Trim();
        string value = Regex.Replace(m.Groups["value"].Value, @"<[^>]*>", "").Trim();
        ht.Add(key, value);
    }foreach (DictionaryEntry de in ht)
    {
        richTextBox2.Text += de.Key.ToString() + "  " + de.Value.ToString() + "\n";
    }