【发布时间】:2014-10-22 21:19:23
【问题描述】:
有没有办法解决这个问题?
我想要一个忽略 tr 标签中所有 td 标签的正则表达式。 我正在寻找的 tr 标签不正确,因为结束标签缺少 “/”。到目前为止,我有:
<tr[^>]*><td(?:(?!</td>).)*</td><tr[^>]*>
<tr[^>]*> This needs to be the beginning of the expression ****
<td(?:(?!</td>).)*</td> This will find everything between <td> and </td>
<tr[^>]*> This needs to be the end of the expression ****
这个正则表达式当然不起作用。以下是运行正则表达式的文本示例:
样本 1:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
</title>
</head>
<body>
<table asdf>
<tr asdf>
<td asdf>
<table asdf>
<tr asdf: asdf>
<td>
blah blah blah
</td>
</tr>
</table>
</td>
<td>
Keep going
</td>
<tr> If highlighted to here from first tr tag than correct regex was used
</table>
</body>
</html>
示例 2:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
</title>
</head>
<body>
<table asdf>
<tr asdf>
<td asdf>
<table asdf>
<tr asdf: asdf>
<td>
blah blah blah
</td>
</tr>
</table>
</td>
<td>
<table asdf>
<tr asdf: asdf>
<td>
blah blah blah
</td>
</tr>
</table>
</td>
<tr> If highlighted to here from first tr tag than correct regex was used
</table>
</body>
</html>
示例 3:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
</title>
</head>
<body>
<table asdf>
<tr asdf>
<td asdf>
<table asdf>
<tr asdf: asdf>
<td>
blah blah blah
</td>
</tr>
</table>
</td>
<td>
<table>
<tr>
<td>
blah blah blah
</td>
</tr>
</table>
</td>
<tr> If highlighted to here from first tr tag than correct regex was used
</table>
</body>
</html>
示例 4:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
</title>
</head>
<body>
<table>
<tr>
<td> </td>
</tr>
</table>
<br/>
<br/>
<br/>
<table class="afdadsf">
<td></td>
</table>
<br/>
<br/>
<table class="fdafdas">
<tr><td></td>
</tr>
</table>
</body>
</html>
我想要的输出是当执行正则表达式时,使用上面的两个示例文本突出显示第一个 tr 标记直到最后一个 tr 标记。假设 td 标签可能包含任何值的其他示例文本。
【问题讨论】:
-
您想忽略
<tr>中的所有<td>标签,但是缺少</tr>不是问题吗?您可能应该包括您想要的输出。 -
我不认为缺少 是问题所在。我需要在正则表达式的结尾和开头有
]*>,因为我正在寻找不正确地以 结尾的 标记。示例文本的代码部分解释了我想要的输出。我将编辑我的评论并将其移到代码部分之外。