请这段文字的正则表达式怎么写

petit 2009-12-27 06:52:36
内容是:
<tr><td width=80 height=30 rowspan=2 align="center">潮州</td><td width=60 height=30 align="center">潮州</td><td width=100 height=30 align="center">2009-12-27 15:40</td><td width=80 height=30 align="center"><img border=0 src="hl12.jpg" width=60 height=50></td><td width=100 height=30 align="center">-----</td><td width=80 height=30 align="center">----</td><td width=100 height=30 align="center">-----</td><td width=80 height=30 align="center">----</td><td width=100 height=30 align="center">-----</td><td width=80 height=30 align="center">----</td><td width=100 height=30 align="center">-----</td><td width=80 height=30 align="center">----</td><td width=100 height=30 align="center">-----</td><td width=80 height=30 align="center">----</td><td width=100 height=30 align="center">-----</td><td width=80 height=30 align="center">----</td><td width=100 height=30 align="center">-----</td><td width=80 height=30 align="center">----</td><td width=100 height=30 align="center">-----</td><td width=80 height=30 align="center">----</td><td width=100 height=30 align="center">-----</td><td width=80 height=30 align="center">----</td></tr>

需求是,把所有<td...>..</td>间的内容都匹配出来,请问正则表达式怎么写
...全文
125 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
petit 2010-01-14
  • 打赏
  • 举报
回复
谢谢各位,正解就是非贪婪
thy38 2009-12-28
  • 打赏
  • 举报
回复
饿死了,下午来看看
ht8269 2009-12-28
  • 打赏
  • 举报
回复
楼上说的很对,不过我确实是看错了题目,你的解法很简洁
thy38 2009-12-28
  • 打赏
  • 举报
回复
写完了想想,其实最后那个环视也不用了,非贪婪就行了。
res = re.findall(r'<td[^>]*>(.*?)</td>', str)
thy38 2009-12-28
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ht8269 的回复:]
python 写法:

import re
res = re.search(' <td[^>]*>(.*) </td>', you_string)
print res.groups(0)[0]
[/Quote]
不知道你有没有测试,总之我测试了不对。因为你这里的'.'是贪婪的,会一直匹配到最后一个</td>才结束。另外,你用的是search,就算前面不是贪婪的,你也只能匹配一个。这种对待问题的态度要不得啊。

我写的是测试了的:
import re

str = ur'''<tr> <td width=80 height=30 rowspan=2 align="center">潮州 </td> <td width=60 height=30 align="center">潮州 </td> <td width=100 height=30 align="center">2009-12-27 15:40 </td> <td width=80 height=30 align="center"> <img border=0 src="hl12.jpg" width=60 height=50> </td> <td width=100 height=30 align="center">----- </td> <td width=80 height=30 align="center">---- </td> <td width=100 height=30 align="center">----- </td> <td width=80 height=30 align="center">---- </td> <td width=100 height=30 align="center">----- </td> <td width=80 height=30 align="center">---- </td> <td width=100 height=30 align="center">----- </td> <td width=80 height=30 align="center">---- </td> <td width=100 height=30 align="center">----- </td> <td width=80 height=30 align="center">---- </td> <td width=100 height=30 align="center">----- </td> <td width=80 height=30 align="center">---- </td> <td width=100 height=30 align="center">----- </td> <td width=80 height=30 align="center">---- </td> <td width=100 height=30 align="center">----- </td> <td width=80 height=30 align="center">---- </td> <td width=100 height=30 align="center">----- </td> <td width=80 height=30 align="center">---- </td> </tr>'''
res = re.findall('<td[^>]*>(.*?)(?!</td>) </td>', str)
for i in res:
print i.strip()

输出:
潮州
潮州
2009-12-27 15:40
<img border=0 src="hl12.jpg" width=60 height=50>
-----
----
-----
----
-----
----
-----
----
-----
----
-----
----
-----
----
-----
----
-----
----
ht8269 2009-12-28
  • 打赏
  • 举报
回复
python 写法:

import re
res = re.search('<td[^>]*>(.*)</td>', you_string)
print res.groups(0)[0]
fanasy2009 2009-12-27
  • 打赏
  • 举报
回复
<td.\{-}<\/td>
在vim中测试过
dskit 2009-12-27
  • 打赏
  • 举报
回复
^(<td>).(*)(</td>)$
我是菜鸟,你试试看
petit 2009-12-27
  • 打赏
  • 举报
回复
自己顶

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

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