如何使用Perl删除HTML文件中的p元素的所有属性? [英] How can I remove all attributes of p elements in HTML files using Perl?

查看:234
本文介绍了如何使用Perl删除HTML文件中的p元素的所有属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用这个简单的Perl命令行来删除HTML文件中< p> 的所有属性:

I'd like to remove all attributes of <p> in an HTML file by using this simple Perl command line:

$ perl -pe 's/<p[^>]*>/<p>/' input.html

但是,它不会替换eg < p class =hello> 跨多行,例如

However, it won't substitute e.g. <p class="hello"> that spans multiple lines such as

<p 
class="hello">

因此,我试图通过执行

# command-1
$ perl -pe 's/\n/ /' input.html > input-tmp.html
# command-2
$ perl -pe 's/<p[^>]*>/<p>/g' input-tmp.html > input-final.html

问题:


  1. 在(Perl)regex中有一个选项可以尝试多行匹配吗?

  2. 我可以结合上面的两个命令和命令-2 )合并为一个?基本上,第一个命令需要在第二个命令开始之前完成执行。

  1. Is there an option in (Perl) regex to try the match across multiple lines?
  2. Can I combine the two commands above (command-1 and command-2) into one? Basically, the first command needs to complete execution before the second one starts.


推荐答案

-p

LINE: while (<>) {
   ...
} continue {
   print
      or die "-p destination: $!\n";
}

正如你可以看到 $ _ 每次只包含一行,因此模式不能匹配跨越多行的某个行。你可以愚弄Perl,使用 -0777 来考虑整个文件是一行。

As you can see $_ only contains one line at a times, so the pattern can't possibly match something that spans more than one line. You can fool Perl into thinking the whole file is one line using -0777.

perl -0777 -pe's/<p[^>]*>/<p>/g' input.html

命令行选项记录在 perlrun 中。

这篇关于如何使用Perl删除HTML文件中的p元素的所有属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆