这里有这样一个问题,我们会遇到这样的情况:
<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>
但是这显然是不能满足我们的需求的,能找到正确的方案吗?
ringa_lee2017-04-17 17:53:26
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.
<td>(.+?)(<a href="(.*?)>(.*?)</a>)?(.+ ?)</td>
#🎜🎜#
#🎜🎜#+
represents at least 1 match, while *
represents 0 or more times #🎜🎜#