求教高手,关于正则表达式
请问下面的例子如何设置正则表达式?
<tr><td>20120409289</td>
<td>2012-04-09 14:24</td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td class="tdb_1"><div>14</div></td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td class="tdb_2"><div>白</div></td><td class="tdb_1"><div>绿</div></td><td> </td><td class="tdb_1"><div>蓝</div></td><td> </td></tr>
<tr><td>20120409288</td>
<td>2012-04-09 14:21</td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td class="tdb_1"><div>14</div></td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td class="tdb_2"><div>白</div></td><td class="tdb_1"><div>绿</div></td><td> </td><td class="tdb_1"><div>蓝</div></td><td> </td></tr>
<tr><td>20120409287</td>
<td>2012-04-09 14:18</td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td class="tdb_1"><div>17</div></td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td class="tdb_1"><div>黑</div></td><td> </td><td class="tdb_1"><div>绿</div></td><td> </td><td class="tdb_1"><div>蓝</div></td><td> </td></tr>
我想获取的是第一对td中的值,第二对td中的值。
还有的就是第3至第30对td中,只有其中一对会出现值。
例如第一段中是第17对td中有值,值为<div>14</div>,最终需要取的值是14
第二段中是第17对td中有值,值为<div>14</div>,最终需要取的值是14
第三段中是第20对td中有值,值为<div>17</div>,最终需要取的值是17
每段后面的其他三个属性,白,绿,蓝等属性不用获取
[解决办法]
<% str = "<tr><td>20120409289</td><td>2012-04-09 14:24</td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td class=""tdb_1""><div>14</div></td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td class=""tdb_2""><div>白</div></td><td class=""tdb_1""><div>绿</div></td><td> </td><td class=""tdb_1""><div>蓝</div></td><td> </td></tr>" 'response.write str set re = new RegExp re.Pattern = "<td[^>]*>(<div>)?((?!\s).+?)(<\/div>)?</td>" re.IgnoreCase = True re.Global = True a = re.replace(re.execute(str)(0), "$2") b = re.replace(re.execute(str)(1), "$2") c = re.replace(re.execute(str)(2), "$2") response.write a response.write "<br/>" response.write b response.write "<br/>" response.write c %>
[解决办法]
Function ExpStr(vStr,vReg,sStr) Dim Re,Matchs,Item,tStr tStr = "" Set Re = New RegExp Re.Pattern = vReg Re.IgnoreCase = True Re.Global = True If Re.Test(vStr) Then Set Matchs = Re.Execute(vStr) For Each Item In Matchs tStr = tStr & Item.Value & sStr Next tStr = Split(Left(tStr,Len(tStr) - Len(sStr)),sStr) End If Set Re = Nothing ExpStr = tStrEnd FunctionFunction RegReplace(vStr,vReg,vCode) Dim Re,sStr sStr = vStr Set Re = New RegExp Re.Pattern = vReg Re.IgnoreCase = True Re.Global = True If Re.Test(sStr) Then sStr = Re.Replace(sStr,vCode) End If RegReplace = sStrEnd Functionstr = "<tr><td>20120409289</td><td>2012-04-09 14:24</td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td class="tdb_1"><div>14</div></td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td class="tdb_2"><div>白</div></td><td class="tdb_1"><div>绿</div></td><td> </td><td class="tdb_1"><div>蓝</div></td><td> </td></tr><tr><td>20120409288</td> <td>2012-04-09 14:21</td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td class="tdb_1"><div>14</div></td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td class="tdb_2"><div>白</div></td><td class="tdb_1"><div>绿</div></td><td> </td><td class="tdb_1"><div>蓝</div></td><td> </td></tr><tr><td>20120409287</td> <td>2012-04-09 14:18</td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td class="tdb_1"><div>17</div></td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td class="tdb_1"><div>黑</div></td><td> </td><td class="tdb_1"><div>绿</div></td><td> </td><td class="tdb_1"><div>蓝</div></td><td> </td></tr>"dim tr,tdtr = ExpStr(str,"<tr[^>]*?>[\s\S]*?</tr>","||")if isarray(tr) then for i = 0 to ubound(tr) td = ExpStr(tr(i),"<td[^<>]*?>[\s\S]*?</td>","||") if isarray(td) then for j = 0 to ubound(td) result = RegReplace(td(j),"<[^<>]*?>","") if trim(result) <> "" then response.write result next end if response.write "<hr>" nextend if
[解决办法]
string patern=@"(?is)<tr>(\s*<td>.*?</td>\s*){16}<td\s*class=""tdb_1""><div>(.*?)</div></td>(\s*<td>.*?</td>\s*){1,}</tr>";
取Group[2].Value就是你要的值