(正则表达式)"过客"者正则表达式高手来帮忙解决下。。。
需求:1 得到有用的TABLE的TD里面的值 显示成:上市时间@2003你8月@手机制式@CDMA 后面的接着。。
注意:需要解析的字符串不都是有用的TABLE
================解析对象有点厂分几部分贴出================
<!--基本功能--开始-->
<!--基本功能--标题--开始-->
<table width= "98% " cellpadding= "0 " cellspacing= "0 ">
<tr>
<td width= "20 " height= "22 " style= "padding:2px 0px 0px 0px; ">
<img src= "/Mobile/Image/icon_circle.gif " width= "16 " height= "16 " border= "0 " /> </td>
<td height= "22 " style= "padding:4px 0px 0px 0px; ">
<font class= 'dataTitleBlackFont '> 基本功能 </font> </td>
</tr>
</table>
<table width= "99% " cellpadding= "0 " cellspacing= "0 ">
<tr>
<td width= "154 " bgcolor= "#6DCDFD "> <img width= "0 " height= "1 " /> </td>
<td bgcolor= "#cccccc "> <img width= "0 " height= "1 " /> </td>
</tr>
<tr> <td colspan= "2 "> <img width= "0 " height= "4 " /> </td> </tr>
</table>
<!--基本功能--标题--结束-->
<!--基本功能--字段--开始-->
<table width= '98% ' cellpadding= '1 ' cellspacing= '0 '>
<tr> <td class= 'dataItemTd '> 『 <a href= '/Mobile/Dictionary/1.htm ' class= 'dataDictionaryLink ' target= '_blank '> CDMA手机 </a> 』 </td> <td class= 'dataItemTd '> 『 <a href= '/Mobile/Dictionary/13.htm ' class= 'dataDictionaryLink ' target= '_blank '> 可选铃声 </a> 』 </td> <td class= 'dataItemTd '> 『 <a href= '/Mobile/Dictionary/16.htm ' class= 'dataDictionaryLink ' target= '_blank '> 图形菜单 </a> 』 </td> <td class= 'dataItemTd '> 『 <a href= '/Mobile/Dictionary/18.htm ' class= 'dataDictionaryLink ' target= '_blank '> 待机图片 </a> 』 </td> <td class= 'dataItemTd '> 『 <a href= '/Mobile/Dictionary/23.htm ' class= 'dataDictionaryLink ' target= '_blank '> 免提通话 </a> 』 </td> </tr> <tr> </tr>
</table>
<!--基本功能--字段--结束-->
<!--基本功能--详细介绍--开始-->
<table width= '604 ' cellpadding= '2 ' cellspacing= '0 '>
<tr> <td colspan= "2 "> <img width= "0 " height= "8 " /> </td> </tr>
<tr>
<td class= "dataLeft1Td " valign= "top "> <a href= '/Mobile/Dictionary/5.htm ' class= 'dataDictionaryLink ' target= '_blank '> 上市日期 </a> : </td>
<td class= "dataRight1Td " valign= "top "> 2003年8月 </td>
</tr>
<tr>
<td class= "dataLeft1Td " valign= "top "> <a href= '/Mobile/Dictionary/6.htm ' class= 'dataDictionaryLink ' target= '_blank '> 手机制式 </a> : </td>
<td class= "dataRight1Td " valign= "top "> CDMA </td>
</tr>
<tr>
<td class= "dataLeft1Td " valign= "top "> <a href= '/Mobile/Dictionary/7.htm ' class= 'dataDictionaryLink ' target= '_blank '> 支持频段 </a> : </td>
<td class= "dataRight1Td " valign= "top "> |CDMA1x-2000| </td>
</tr>
<tr>
<td class= "dataLeft1Td " valign= "top "> <a href= '/Mobile/Dictionary/9.htm ' class= 'dataDictionaryLink ' target= '_blank '> 理论通话时间 </a> : </td>
<td class= "dataRight1Td " valign= "top "> 80-120分钟 </td>
</tr>
<tr>
<td class= "dataLeft1Td " valign= "top "> <a href= '/Mobile/Dictionary/10.htm ' class= 'dataDictionaryLink ' target= '_blank '> 理论待机时间 </a> : </td>
<td class= "dataRight1Td " valign= "top "> 60-100小时 </td>
</tr>
<tr>
<td class= "dataLeft1Td " valign= "top "> <a href= '/Mobile/Dictionary/11.htm ' class= 'dataDictionaryLink ' target= '_blank '> 标准配置 </a> : </td>
<td class= "dataRight1Td " valign= "top "> 两锂电池(600mAh)、旅行充电器、充电座、外置摄像头、戒指耳机、铝盒、铝制包装箱一个 </td>
</tr>
<tr>
<td class= "dataLeft1Td " valign= "top "> <a href= '/Mobile/Dictionary/23.htm ' class= 'dataDictionaryLink ' target= '_blank '> 免提通话 </a> : </td>
<td class= "dataRight1Td " valign= "top "> 支持 </td>
</tr>
<tr> <td colspan= "2 "> <img width= "0 " height= "8 " /> </td> </tr>
</table>
<!--基本功能--详细介绍--结束-->
<!--基本功能--结束-->
[解决办法]
我也来凑凑热闹....
ArrayList arry = new ArrayList(); //存放结果
string str = "你的源代码 ";
Regex re = new Regex(@ " <tr> \s* <td\s+class=\ " "dataLeft\dTd\ " "\s+valign=\ " "top\ " "> \s* <a\s+href= '[^ ']+ '\s*class= 'dataDictionarylink '[^> ]+> (? <var1> [\S\s]+?) </a> :\s* </td> \s* <td\s+class=\ " "dataRight\dTd\ " "[^> ]+> (? <var2> [\S\s]+?) </td> \s* </tr> ", RegexOptions.IgnoreCase);
MatchCollection mc = re.Matches(str);
foreach (Match ma in mc)
{
arry.Add(ma.Groups[ "var1 "].Value + ": "+ma.Groups[ "var2 "].Value);
}
foreach (string strs in arry) //输出结果
{
Response.Write(strs + " <br> ");
}
[解决办法]
这个是结果:
上市日期:2003年8月
手机制式:CDMA
支持频段:|CDMA1x-2000|
理论通话时间:80-120分钟
理论待机时间:60-100小时
标准配置:两锂电池(600mAh)、旅行充电器、充电座、外置摄像头、戒指耳机、铝盒、铝制包装箱一个
免提通话:支持
外观设计:手表外型
产品天线:外置
机身颜色:亮丽黑、魔幻蓝
产品尺寸:77.5×33.8×17mm
手机重量:95克
可选颜色:亮丽黑、魔幻蓝
屏幕颜色:256色
屏幕类型:STN
输入法:中文输入法;
话机通讯录:支持
通话记录:支持
留言应答:支持
语音拨号:支持
摄像头位置:外置
像素:30万
传感器:CMOS
录音:4分钟录音
日历:支持
计算器:支持
闹钟:支持
备忘录:支持
[解决办法]
你测一下吧,这一条语句基本不会造成什么性能损失的,我那样写只是为了照顾通用性