如何用regex清单替换xml

如何用regex清单替换xml,regex,perl,Regex,Perl,我有一个xml文件和一些替换xml文件的替换检查列表。如何转义正则表达式并替换该xml文件。只是我尝试过这个概念,但它不能完美地工作。。。我该怎么做 我试过: 输入xml: <xml> <p class="text">The <em type="italic">end</em> of the text</p> <p class="text">The <bold type="strong">end of the&l

我有一个xml文件和一些替换xml文件的替换检查列表。如何转义正则表达式并替换该xml文件。只是我尝试过这个概念,但它不能完美地工作。。。我该怎么做

我试过:

输入xml:

<xml>
<p class="text">The <em type="italic">end</em> of the text</p>
<p class="text">The <bold type="strong">end of the</bold> text</p>
<p class="text">The end of <samll type="caps">the<small> text</p>
</xml>

文本的结尾

文本的结尾

文本的结尾

脚本:

use strict;
open(IN, "xml_file.xml") || die "can't open $!";
my $text = join '', <IN>;
my @ar = '';
my $testing;
foreach my $t (<DATA>){
    @ar = split /\t/, $t;
    chomp($ar[0]);
    chomp($ar[1]);
    $text =~ s/$ar[0]/$ar[1]/segi;
}
print $text;

__END__
<p([^>]+)?> <line>
<small([^>]+)?> <sc$1>
<bold type=\"([^"]+)\"> <strong act=\"$1\">
<(\/)?em([^>]+)?>   <$1emhasis$2>
使用严格;
打开(在“xml_file.xml”中)| | die“无法打开$!”;
我的$text=加入“”;
我的@ar='';
我的$testing;
每台$t(){
@ar=拆分/\t/,$t;
chomp($ar[0]);
chomp($ar[1]);
$text=~s/$ar[0]/$ar[1]/segi;
}
打印$text;
__结束__
]+)?> 
]+)?> 
只需添加:

$ar[0] = qr/$ar[0]/;
在执行regexpr替换之前

另外,您忘记了这个模式:

</p>    </line>

输入xml中有输入错误:

<samll type="caps">

应该是

<small type="caps">

最后,有一条建议:用正则表达式解析XML不是一个好主意。我建议使用来自CPAN的XML解析器,这是一个更好的选择(IMO)。

对于an,您需要使用双eval替换

我无法使用
,但下面的代码可以工作。您可以根据需要创建@replace结构,我刚刚创建了一个简单的结构

my $text = <<XML;
<xml>
<p class="text">The <em type="italic">end</em> of the text</p>
<p class="text">The <bold type="strong">end of the</bold> text</p>
<p class="text">The end of <small type="caps">the</small> text</p>
</xml>
XML

my @replace = (
    {
        'select' => '<p([^>]+)?>',
        'replace' => '"<line$1>"'
    },
    {
        'select' => '/p>',
        'replace' => '"/line>"'
    },
    {
        'select' => '<small([^>]+)?>',
        'replace' => '"<sc$1>"'
    },
    {
        'select' => '/small>',
        'replace' => '"/sc>"'
    },
    {
        'select' => '<bold\s+type="(.+?)".*?>',
        'replace' => '"<strong act=\"$1\">"'
    },
    {
        'select' => '/bold>',
        'replace' => '"/strong>"'
    },
    {
        'select' => '<em([^>]+)?>',
        'replace' => '"<emhasis$1>"'
    },
    {
        'select' => '/em>',
        'replace' => '"/emhasis>"'
    },
);

map {my $re = $_; $text =~ s/$re->{select}/$re->{replace}/sigee;} @replace;

print $text;
my$text='“”
},
{
'选择'=>'/p>',
'替换'=>'”/line>“'
},
{
'选择'=>']+)?>',
“替换“=>”“”
},
{
'选择'=>'/small>',
'替换'=>'“/sc>“'
},
{
'选择'=>'',
'替换'=>'“”'
},
{
'选择'=>'/bold>',
'替换'=>'”/strong>“'
},
{
'选择'=>']+)?>',
“替换“=>”“”
},
{
'选择'=>'/em>',
'替换'=>'“/emhasis>“'
},
);
映射{my$re=$\;$text=~s/$re->{select}/$re->{replace}/sigee;}@replace;
打印$text;

运行此操作时,您实际得到了什么输出?
$text=~s/$ar[1]/$ar[1]/segi???你的回答是错误的。我想您需要
$text=~s/$ar[0]/$ar[1]/segi哦,你咬错了,它是
咬($ar[0]);chomp($ar[1])
not
1,2
数组以0开始,而不是以1开始@Paulchenkiller所说的似乎很有可能,但实际上,有太多可能的问题,除非我们看到您的输出,否则很难准确地告诉您发生了什么,以寻求建议。但我已经尝试过如何从regex模式中获取,以及如何在replace@Mr.吉钦很好地回答了这个问题。。非常感谢。它工作得很好。。。
my $text = <<XML;
<xml>
<p class="text">The <em type="italic">end</em> of the text</p>
<p class="text">The <bold type="strong">end of the</bold> text</p>
<p class="text">The end of <small type="caps">the</small> text</p>
</xml>
XML

my @replace = (
    {
        'select' => '<p([^>]+)?>',
        'replace' => '"<line$1>"'
    },
    {
        'select' => '/p>',
        'replace' => '"/line>"'
    },
    {
        'select' => '<small([^>]+)?>',
        'replace' => '"<sc$1>"'
    },
    {
        'select' => '/small>',
        'replace' => '"/sc>"'
    },
    {
        'select' => '<bold\s+type="(.+?)".*?>',
        'replace' => '"<strong act=\"$1\">"'
    },
    {
        'select' => '/bold>',
        'replace' => '"/strong>"'
    },
    {
        'select' => '<em([^>]+)?>',
        'replace' => '"<emhasis$1>"'
    },
    {
        'select' => '/em>',
        'replace' => '"/emhasis>"'
    },
);

map {my $re = $_; $text =~ s/$re->{select}/$re->{replace}/sigee;} @replace;

print $text;