【问题标题】:Sublime Text 3 - Find and remove multiple </section> searching by "id" in multiple filesSublime Text 3 - 在多个文件中通过“id”查找并删除多个</section>搜索
【发布时间】:2021-09-03 02:18:40
【问题描述】:

我有几个具有相同 &lt;section&gt; 但内容不同的 html 文件。

我想知道是否可以使用 sublime 文本删除多个文件中的这些部分

示例:

<section class="all-classes" id="section1">
     content 
</section>
<section class="all-classes" id="section2-do-not-remove-section">
     content 
</section>
<section class="all-classes" id="section3">
     content 
</section>
<section class="all-classes" id="section4">
     content 
</section>

在本例中,我想删除第 1、3 和 4 节并保留第 2 节

【问题讨论】:

  • 您要删除所有id格式为section#的部分,其中#为1,2,3,4,5,6,7,8,9,10,11 ,... ?
  • @Ouroborus 来自What topics can I ask about here? 中的help center,软件问题如果涵盖“[...] 程序员常用的软件工具”,则允许。 Sublime Text和 Vim、Emacs、VSCode 等一样,是一个编程编辑器,在这个网站上有关于它们的 tens of thousands of questions 完全符合主题。此外,这是一个编程问题,因为答案是使用 HTML 解析器。
  • 这实际上是 HTML 解析器的工作,而不是正则表达式。请参阅this 以获得好笑,但也有一些很好的答案来解释为什么正则表达式不是这项工作的工具。 BeautifulSoup4lxml 是 Python 的首选工具,我不知道其他语言。

标签: html regex sublimetext3 sublimetext


【解决方案1】:

正如 MattDMo 在 cmets 中提到的,HTML 解析器将是您完成这项工作的最佳选择。

对于只需要快速查找+替换的简单情况,您可以使用这个 RegEx:

<section.*id="section[\d]*"[\s\S]*?<\/section>

看到它在行动here

地点:

  • &lt;section.* - 捕获以标签 section 开头的文本,例如&lt;section class="all-classes"
  • id="section[\d]*" - 捕获名称为 section 后跟数字的 id,例如id="section32"
  • [\s\S]*? - 以非贪婪的方式捕获所有字符(空格与否)。这是为了防止跨越多个部分。
  • &lt;\/section&gt; - 抓住结束标签&lt;/section&gt;。由于这是以非贪婪的方式捕获的,因此这将始终是最接近的 &lt;/section&gt; 标记。

警告:如果您有嵌套的部分(部分中的部分),这将不起作用。为此,您必须使用 HTML 解析器。

【讨论】:

    猜你喜欢
    • 2017-08-27
    • 2019-04-10
    • 2015-04-29
    • 1970-01-01
    • 2014-10-30
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 2019-03-19
    相关资源
    最近更新 更多