正则表达式匹配方法求解[50分]

potter2002 2010-04-12 03:37:37
<table>
<tr>
<th>姓名</th>
<th>身高</th>
<th>年纪</th>
<th>学历</th>
</tr>

<tr>
<td>张三</td>
<td>198厘米</td>
<td>25</td>
<td>小学</td>
</tr>

<tr>
<td>李四</td>
<td>178厘米</td>
<td>18</td>
<td>大学</td>
</tr>
</table>

用正则表达式得出以下结果

序号:1
姓名:张四
身高:198厘米
年纪:25
学历:小学

序号:2
姓名:李四
身高:178厘米
年纪:18
学历:大学

需要详细代码,谢谢
...全文
136 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
catici 2010-04-13
  • 打赏
  • 举报
回复
貌似可以直接用VS2005生成代码的吧?
yan11cn 2010-04-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 potter2002 的回复:]
<table>
<tr>
<th width="19" stype="color=red">姓名</th>
<th width="18">身高</th>
<th width="16">年纪</th>
<th width="20">学历</th>
</tr>

<tr>
<td width="19">张三</td>
<td width="29">1……
[/Quote]

稍微修改下就行:

<%
Dim str,reg1,reg2
str="<table><tr><th>姓名</th><th>身高</th><th>年纪</th><th>学历</th> </tr><tr><td>张三</td><td>198厘米</td><td>25</td> <td>小学</td></tr><tr><td>李四</td><td>178厘米</td><td>18</td><td>大学</td></tr></table>"
set reg1 = New RegExp
set reg2 = New RegExp
reg1.Global=true
reg1.Pattern="<th.*?>([\W\w]*?)<\/th>"
reg1.IgnoreCase = True
reg2.Global=true
reg2.Pattern="<td.*?>([\W\w]*?)<\/td>"
reg2.IgnoreCase = True
Set matches1=reg1.execute(str)
Set matches2=reg2.execute(str)
Dim num
num=matches1.count
max=matches2.count / matches1.count
For i=0 To max-1
response.write "序号:" & i+1 &"<br />"
For j=0 To num-1
response.write matches1(j).value &":" & matches2(i*num+j).value &"<br />"
Next
response.write "<br />"
Next
%>
  • 打赏
  • 举报
回复
那过滤一下即可
potter2002 2010-04-12
  • 打赏
  • 举报
回复
我想要的结果是(除了<br>不要用其它html代码)

序号:1
姓名:张四
身高:198厘米
年纪:25
学历:小学

序号:2
姓名:李四
身高:178厘米
年纪:18
学历:大学
potter2002 2010-04-12
  • 打赏
  • 举报
回复
to chinmo 不好意思,我描述错误。我的意思是还有有那些html代码存在

结果
序号:1<br /><th width="19" stype="color=red">姓名</th>:<td>张三</td><br /><th>身高</th>:<td>198厘米</td><br /><th>年纪</th>:<td>25</td><br /><th>学历</th>:<td>小学</td><br /><br />序号:2<br /><th width="19" stype="color=red">姓名</th>:<td>李四</td><br /><th>身高</th>:<td>178厘米</td><br /><th>年纪</th>:<td>18</td><br /><th>学历</th>:<td>大学</td><br /><br />
  • 打赏
  • 举报
回复
我运行好好的,怎么说报错?
potter2002 2010-04-12
  • 打赏
  • 举报
回复
to chinmo,报错。
  • 打赏
  • 举报
回复
汗!很简单的
<%
Dim str,reg1,reg2
str="<table><tr><th width=""19"" stype=""color=red"">姓名</th><th>身高</th><th>年纪</th><th>学历</th> </tr><tr><td>张三</td><td>198厘米</td><td>25</td> <td>小学</td></tr><tr><td>李四</td><td>178厘米</td><td>18</td><td>大学</td></tr></table>"
set reg1 = New RegExp
set reg2 = New RegExp
reg1.Global=true
reg1.Pattern="<th[^>]*>([\W\w]*?)<\/th>"
reg1.IgnoreCase = True
reg2.Global=true
reg2.Pattern="<td[^>]*>([\W\w]*?)<\/td>"
reg2.IgnoreCase = True
Set matches1=reg1.execute(str)
Set matches2=reg2.execute(str)
Dim num
num=matches1.count
max=matches2.count / matches1.count
For i=0 To max-1
response.write "序号:" & i+1 &"<br />"
For j=0 To num-1
response.write matches1(j).value &":" & matches2(i*num+j).value &"<br />"
Next
response.write "<br />"
Next
%>
potter2002 2010-04-12
  • 打赏
  • 举报
回复
<table>
<tr>
<th width="19" stype="color=red">姓名</th>
<th width="18">身高</th>
<th width="16">年纪</th>
<th width="20">学历</th>
</tr>

<tr>
<td width="19">张三</td>
<td width="29">198厘米</td>
<td width="17">25</td>
<td width="15">小学</td>
</tr>

<tr>
<td width="19">李四</td>
<td width="29">178厘米</td>
<td width="17">18</td>
<td width="15">大学</td>
</tr>
</table>


如果变成这样呢,非常感谢yan11cn 我突然发现还有这样的问题,请继续关注。谢谢
yan11cn 2010-04-12
  • 打赏
  • 举报
回复

<%
Dim str,reg1,reg2
str="<table><tr><th>姓名</th><th>身高</th><th>年纪</th><th>学历</th> </tr><tr><td>张三</td><td>198厘米</td><td>25</td> <td>小学</td></tr><tr><td>李四</td><td>178厘米</td><td>18</td><td>大学</td></tr></table>"
set reg1 = New RegExp
set reg2 = New RegExp
reg1.Global=true
reg1.Pattern="<th>([\W\w]*?)<\/th>"
reg1.IgnoreCase = True
reg2.Global=true
reg2.Pattern="<td>([\W\w]*?)<\/td>"
reg2.IgnoreCase = True
Set matches1=reg1.execute(str)
Set matches2=reg2.execute(str)
Dim num
num=matches1.count
max=matches2.count / matches1.count
For i=0 To max-1
response.write "序号:" & i+1 &"<br />"
For j=0 To num-1
response.write matches1(j).value &":" & matches2(i*num+j).value &"<br />"
Next
response.write "<br />"
Next
%>
potter2002 2010-04-12
  • 打赏
  • 举报
回复
split怎么样弄呢?
lzp4881 2010-04-12
  • 打赏
  • 举报
回复
用正则?
<td>张三</td>
<td>李四</td>
这两个一样,怎么区分啊。

用split很简单就可以解决,不要把简单的问题复杂化。

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧