3

タグのhref属性に不正な構文を含むHTMLファイルのセットがあります。<a>例えば、

<a name="Conductor, "neutral""></a>

また

<meta name="keywords" content="Conductor, "hot",Conductor, "neutral",Hot wire,Neutral wire,Double insulation,Conductor, "ground",Ground fault,GFCI,Ground Fault Current Interrupter,Ground fault,GFCI,Ground Fault Current Interrupter,Arc fault circuit interrupter,Arc fault breaker,AFCI," />

また

<b>Table of Contents:</b><ul class="xoxo"><li><a href="1.html" title="Page 1: What are "series" and "parallel" circuits?">What are "series" and "parallel" circuits?</a>

XML::Twigを使用してPerlのモジュールでファイルを処理しようとしていますparsefile_html($file_name)。この構文を持つファイルを読み取ると、次のエラーが発生します。

x has an invalid attribute name 'y""' at C:/strawberry/perl/site/lib/XML/Twig.pm line 893

私が必要としているのは、モジュールに不正な構文を受け入れて処理させる方法、または属性内の二重引用符を見つけて一重引用符に置き換える正規表現のいずれかです。

4

2 に答える 2

2

HTMLサンプルが与えられると、以下のコードが機能します。

use Modern::Perl;

my $html = <<end;
<meta name="keywords" content="Conductor, "hot",Conductor, "neutral",Hot wire,Neutral wire,Double insulation,Conductor, "ground",Ground fault,GFCI,Ground Fault Current Interrupter,Ground fault,GFCI,Ground Fault Current Interrupter,Arc fault circuit interrupter,Arc fault breaker,AFCI," />
<a name="Conductor, "neutral""></a>
end

$html =~ s/(?<=content=")(.*?)(?="\s*\/>)/do{my $capture = $1; $capture =~ s|"||g;$capture}/eg;
$html =~ s/(?<=name=")(.*?)(?="\s*>)/do{my $capture = $1; $capture =~ s|"||g;$capture}/eg;

say $html;

出力:

<meta name="keywords" content="Conductor, hot,Conductor, neutral,Hot wire,Neutral wire,Double insulation,Conductor, ground,Ground fault,GFCI,Ground Fault Current Interrupter,Ground fault,GFCI,Ground Fault Current Interrupter,Arc fault circuit interrupter,Arc fault breaker,AFCI," />
<a name="Conductor, neutral"></a>

可変長後読みが実装されていないので、等号の前後にスペースがあると、パターンマッチングが失敗するのではないかと心配しています。ただし、ページが一貫して作成されている可能性が高いため、一致は失敗しません。

もちろん、最初にファイルのコピーの置換を試してください。

于 2012-05-16T04:09:32.240 に答える
1

これを合理的に安全に行うために私が考えることができる唯一の方法は、2つのネストされた評価済み(/e)置換を使用することです。以下のプログラムはこのアイデアを使用し、データを処理します。

外部置換は、文字列内のすべてのタグを検索し、それらを調整された属性値を含むタグに置き換えます。

内部置換は、タグ内のすべての属性値を検索し、それらを同じ値に置き換えて、すべての二重引用符を削除します。

use strict;
use warnings;

my $html = <<'HTML';
<meta name="keywords" content="Conductor, "hot",Conductor, "neutral",Hot wire,Neutral wire,Double insulation,Conductor, "ground",Ground fault,GFCI,Ground Fault Current Interrupter,Ground fault,GFCI,Ground Fault Current Interrupter,Arc fault circuit interrupter,Arc fault breaker,AFCI," />
<a name="Conductor, "neutral""></a>
<a href="1.html" title="Page 1: What are "series" and "parallel" circuits?">
HTML

$html =~ s{(<[^>]+>)}{

  my $tag = $1;

  $tag =~ s{ \w+= " \K ( [^=<>]+ ) (?= " (?: \s+\w+= | \s*/?> )) }
  {
    (my $attr = $1) =~ tr/"//d;
    $attr;
  }egx;

  $tag;
}eg;

print $html;

出力

<meta name="keywords" content="Conductor, hot,Conductor, neutral,Hot wire,Neutral wire,Double insulation,Conductor, ground,Ground fault,GFCI,Ground Fault Current Interrupter,Ground fault,GFCI,Ground Fault Current Interrupter,Arc fault circuit interrupter,Arc fault breaker,AFCI," />
<a name="Conductor, neutral"></a>
<a href="1.html" title="Page 1: What are series and parallel circuits?">
于 2012-05-16T14:11:10.853 に答える