使用正则表达式在Notepad++中选择和替换多行

3

我有一个非常大的HTML文件,其中包含安全扫描结果,我需要提取文档中的无用信息。我需要提取的内容示例类似于:

<tr>
<td width="20%" valign="top" class="classcell0"><span class="classtext" style="color: #ffffff; font-weight: bold !important;">Info</span></td>
<td width="10%" valign="top" class="classcell"> <a href="http://www.nessus.org/plugins/index.php?view=single&amp;id=10395" target="_blank"> 10395</a>
</td>
<td width="70%" valign="top" class="classcell"><span class="classtext" style="color: #263645; font-weight: normal;">Microsoft Windows SMB Shares Enumeration</span></td>
</tr>

编辑后,上面的文本应该直接删除。由于变化的原因,我无法进行标准查找。以下是另一个需要从文档中删除的示例:
<tr>
<td width="20%" valign="top" class="classcell0"><span class="classtext" style="color: #ffffff; font-weight: bold !important;">Info</span></td>
<td width="10%" valign="top" class="classcell"> <a href="http://www.nessus.org/plugins/index.php?view=single&amp;id=11219" target="_blank"> 11219</a>
</td>
<td width="70%" valign="top" class="classcell"><span class="classtext" style="color: #263645; font-weight: normal;">Nessus SYN scanner</span></td>
</tr>

我需要将ID号码10395作为变量处理,但长度保持不变。同样,“Microsoft Windows SMB Shares Enumeration”也需要被视为变量,因为它在整个文档中会发生变化。

我尝试将类似于以下内容的东西放入替换中,但我认为我完全没有做对。

<td width="10%" valign="top" class="classcell"> <a href="http://www.nessus.org/plugins/index.php?view=single&amp;id=\1\1\1\1\1" target="_blank"> \1\1\1\1\1</a>

也许我应该使用其他工具?

1
你想将什么转换成什么?改变后的文档应该是什么样子?(这是逐行匹配和替换吗?) - Tezra
@Tezra 我只是想要删除那些代码片段,所以只需要用空格或 \n 替换它们。如果我按照目前的思路来处理,每次需要替换 6 行代码。 - creigel
2
所以您想要删除显示文本部分?您能否在问题中添加应该看起来像什么的示例? - Tezra
2个回答

1

从最简单到更复杂的正则表达式,但它们都能完成工作:

<a.*>.*\d.*</a>

<a.*>.*\d{5}.*</a>

<a.*id=\d{5}.*>.*\d{5}.*</a>

免责声明:请小心。我无法使用正则表达式解析html。


这对于单行代码非常有效。谢谢您的回复。 - creigel

1

我猜你在多次重复\1时,意思是它是单个字符的占位符,但实际上并不是这样。你想要实现的是类似于这样的效果:

<td width="10%" valign="top" class="classcell"> <a href="http://www.nessus.org/plugins/index.php?view=single&amp;id=(\d+)" target="_blank"> \1</a>

为了匹配整个6行:
<tr>\s*<td width="20%" valign="top" class="classcell0"><span class="classtext" style="color: #ffffff; font-weight: bold !important;">Info</span></td>\s*<td width="10%" valign="top" class="classcell"> <a href="http://www\.nessus\.org/plugins/index\.php\?view=single&amp;id=(\d+)" target="_blank"> \1</a>\s*</td>\s*<td width="70%" valign="top" class="classcell"><span class="classtext" style="color: #263645; font-weight: normal;">.*?</span></td>\s*</tr>

然后您可以将其替换为空字符串。

非常感谢!完美解决了! - creigel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接