修复包含双引号的HTML属性值

修复包含双引号的HTML属性值,html,regex,perl,text,xml-twig,Html,Regex,Perl,Text,Xml Twig,我在标记的href属性中有一组语法非法的HTML文件。比如说, <a name="Conductor, "neutral""></a> 我需要的是一种让模块接受错误语法并加以处理的方法,或者一个正则表达式来查找并用单引号替换属性中的双引号。给定html示例,下面的代码可以工作: use Modern::Perl; my $html = <<end; <meta name="keywords" content="Conductor, "hot",Con

我在
标记的
href
属性中有一组语法非法的HTML文件。比如说,

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

我需要的是一种让模块接受错误语法并加以处理的方法,或者一个正则表达式来查找并用单引号替换属性中的双引号。

给定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;
使用Modern::Perl;

我的$html=我能想到的唯一合理安全的方法是使用两个嵌套的求值(
/e
)替换。下面的程序使用了这一思想并处理您的数据

外部替换将查找字符串中的所有标记,并将它们替换为包含已调整属性值的标记

内部子替换查找标记中的所有属性值,并用相同的值替换它们,同时删除所有双引号

使用严格;
使用警告;

我的$html=工作得很好!这是一对非常紧张的正则表达式。我会接受你的回答,但是一些评论会很有帮助。哦!对该字符串不太有效:
使用
(?明白了!我会接受的。但是找到一个更通用的解决方案可能会更好…这太棒了,Borodin。你在解决方案中采取的两个步骤很有意义。我曾经在属性值中有一个>。那是一个糟糕的日子,我仍然像这样匹配标记。:)@布莱恩:我猜它们必须被编码为
——即使是在属性中。我还从来没有过这样的一天!
<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>
x has an invalid attribute name 'y""' at C:/strawberry/perl/site/lib/XML/Twig.pm line 893
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>