python - 如何用正则表达式匹配标签里面的a标签
高洛峰
高洛峰 2017-04-17 17:51:26
0
1
674

这里有这样一个问题,我们会遇到这样的情况:

<td>(1)(<a href="(2)">(3)</ a>)(4)</ td>

这里的2,3位置决定的a标签可能存在,而1,4的位置可能由内容也可能没有内容,能不能用一个正则表达式让不管a标签存在与否都匹配出1,4位置的内容呢

比如
<td><a href=""></a>this is not empty</td>
<td>this is not empty<a href=""></a></td>
<td>this is not empty><a href=""></a>this is not empty</td>

这里有一个例子

<td>(.+?)(<a href="(.+?)>(.+?)</a>)?(.+?)</td>

但是这显然是不能满足我们的需求的,能找到正确的方案吗?

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(1)
左手右手慢动作

Haha, this would be easy if it were php. Just filter the tags directly and you’re done. However, PY
depends on what you mean by extracting text from the table. .
That’s okay
<td>(.*?)(<.+?>)*(.*?)</td><td>(.*?)(<.+?>)*(.*?)</td>
也可以分步,先 <td>(.+?)</td> 把内容取出来。然后把 <.+?> 全替换空。

或者。。用回你的正则
<td>(.+?)(<a href="(.*?)>(.*?)</a>)?(.+?)</td>

+代表匹配至少1次,而* You can also do it step by step, first <td>(.+?)</td> to take out the content. Then replace all <.+?> with nothing.

#🎜🎜#Or. . Use your regular expressions#🎜🎜#<td>(.+?)(<a href="(.*?)>(.*?)</a>)?(.+ ?)</td>#🎜🎜# #🎜🎜#+ represents at least 1 match, while * represents 0 or more times #🎜🎜#
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template