【问题标题】:kate - regex - find and replace portion of URL from href to ">kate - 正则表达式 - 查找和替换从 href 到 "> 的部分 URL
【发布时间】:2019-03-15 16:04:42
【问题描述】:

我想修改许多包含在 .html 和 .txt 文件中的链接。我主要使用 Kate 作为我的文本编辑器,因此我用kate 标签提出了这个问题。以下是链接示例:

<li>
  <a href="http://sk1project.org/">
            sK1
        </a> is an open source vector graphics editor similar to CorelDRAW, Adobe Illustrator, or Freehand. First of all sK1 is oriented for PostScript processing. UniConvertor is a universal vector graphics translator. It uses sK1 engine to convert
  one format to another. Development of the import/export modules for this program goes through different stages, quality and feature coverage are different among formats.
</li>


<li>
  <a href="http://tango.freedesktop.org/Tango_Desktop_Project">
            The Tango Desktop Project
        </a> exists to help create a consistent graphical user interface experience for free and Open Source software. While the look and feel of an application is determined by many individual components, some organization is necessary in order to
  unify the appearance and structure of individual icon sets used within those components. The Tango Desktop Project defines an icon style guideline to which artists and designers can adhere. A sample implementation of the style is available as an icon
  theme based upon a standardized icon naming specification. In addition, the project provides transitional utilities to assist in creating icon themes for existing desktop environments, such as GNOME and KDE.
</li>

我发现 Regular expression to extract URL from an HTML link | python - 从 HTML 链接中提取 URL 的正则表达式 - 堆栈溢出,所以我知道如何使用 href=[\'"]?([^\'" &gt;]+"&gt;) 将文本从 href 捕获到“>”,但我不知道如何将文本从 href 保留到“之前> 并添加以下文本:'rel="nofollow noopener noreferrer">'。

我知道最终结果应该如下所示:

<li>
  <a href="http://sk1project.org/" rel="nofollow noopener noreferrer">
            sK1
        </a> is an open source vector graphics editor similar to CorelDRAW, Adobe Illustrator, or Freehand. First of all sK1 is oriented for PostScript processing. UniConvertor is a universal vector graphics translator. It uses sK1 engine to convert
  one format to another. Development of the import/export modules for this program goes through different stages, quality and feature coverage are different among formats.
</li>


<li>
  <a href="http://tango.freedesktop.org/Tango_Desktop_Project" rel="nofollow noopener noreferrer">
            The Tango Desktop Project
        </a> exists to help create a consistent graphical user interface experience for free and Open Source software. While the look and feel of an application is determined by many individual components, some organization is necessary in order to
  unify the appearance and structure of individual icon sets used within those components. The Tango Desktop Project defines an icon style guideline to which artists and designers can adhere. A sample implementation of the style is available as an icon
  theme based upon a standardized icon naming specification. In addition, the project provides transitional utilities to assist in creating icon themes for existing desktop environments, such as GNOME and KDE
</li>

如何在 Kate 中使用正则表达式来完成这项工作?

谢谢。

【问题讨论】:

  • I found Regular expression to extract URL from an HTML link 不,你没有,你发现了垃圾!

标签: html regex replace regex-group kate


【解决方案1】:

不推荐使用正则表达式解析 html,但由于您使用的是 Kate 编辑器,因此您可以使用此正则表达式捕获带有 href 属性的 &lt;a 标记,

(<a\s+.*?href=(['"]?)\S*\2)

并用这个替换它,

\1 rel="nofollow noopener noreferrer"

我从未使用过 Kate 编辑器,所以不确定\1 是否可以使用或$1 是否可以使用

让我知道这是否有效。

Demo

【讨论】:

  • @sln: 有什么不好?你能用一个实际的例子来解释为什么 OP 不能在这里匹配 &lt;a 标签吗?请记住,OP 正在使用编辑器。您在这里提出什么解决方案?
  • 当然。有效的 html 标签不允许引号内的引号。而且您实际上并没有匹配 html 标记内的属性。请参阅我发布的链接。我可以给你一个有效的方法,但你不会明白。
  • 你可以看看这个regex101.com/r/OXLAV9/1。有任何问题,请告诉我。
  • @sln:你在世界上哪里得到那些在 URL 中包含引号的 URL?您已经说过在 URL 中无效?另外,请记住,匹配与验证之间存在很大差异。并不是说 OP 有一个字段,该字段需要仅通过有效 URL 的用户输入进行验证。相反,所有 OP 想要做的只是匹配 URL 并在其中包含额外的属性。
  • @Pushpesh Kumar Rajwanshi 您提供的答案在 Kate 文本编辑器中对我有用。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-09
  • 2012-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多