已知有若干条记录(都是字符串),形式如下:型号=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不知道怎么解决好。不知道用正则可以不可以做出来
每条记录可能只记录其中某几条属性的值。需将每条记录中分离出各个属性的值,如记录中没有该属性的值则其值为空。例如:
型号=Pentium D 805(盒装);适用类型=台式机;生产工艺=0.09um;核心电压=1.3V;
分解出结果应该是
型号 Pentium D 805(盒装)
适用类型 台式机
接口类型
核心类型
生产工艺 0.09um
核心电压 1.3V不知道怎么解决好。不知道用正则可以不可以做出来
解决方案 »
- |zyciis| 我的Form Hide 掉了,然后我用代码显示他里面的一个菜单,任务栏里就多出了菜单的任务栏,能否去掉(有源码)
- 连接数据库碰一“妖怪”,望高手来降“妖”
- 如何动态的将Treeview中Node节点的层次顺序写入数据库
- 求IMLibrary.dll源码
- c# 调用mysql.exe 的问题,急!!
- 程序调试错误
- 求一个利用存储过程+datalist分页的C#的例子(要有源码)
- ,望高手指点怎样学好c#?非常感谢。
- 我想让用户动态提出编码规则,然后程序据此生成所有符合要求的编码
- MdiChildren 是只读,不能赋值?如何使用?只能使用parent ?
- 请教一个关于读取未知类型文件的问题,急!!!!
- 向服务器页面发送请求,乱码问题
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);
}
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;
}
}
}
我尝试用正则去分解,但是一直没搞定,如果可以,请大家帮忙想想
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";
}
只是一个大概的思路,如果满足要求,实现方法自己优化一下
这个问题基本解决了,我是通过拼凑正则表达式的方法来解决的,我觉得我的方法很蠢,不知道还有没有更好的方法。
上面的需求是个简化了的模型,实际情况和我实现的主要代码如下:
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
核心类型:
型号: Pentium D 805(盒装)
适用类型:
接口类型: LGA 775
核心类型:
这种形式就可以了,也就是可以看做一个多行字符串,还是说需要一行一行的,分做多行来输出,或者是分行写入数据库之类的
<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>
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";
}