我将下面所有这些存储在$data中。
'Berry-Berry Belgian Waffles' => {
'calories' => '900',
'price' => '$8.95',
'description' => 'Light Belgian waffles covered with an assortment of fresh berries and whipped cream'
},
我需要使用正则表达式提取'{'和'}'之间的内容。因此,结果应该如下所示:
'calories' => '900',
'price' => '$8.95',
'description' => 'Light Belgian waffles covered with an assortment of fresh berries and whipped cream'
我如何使用perl脚本实现这一点?
这是我到目前为止的脚本,它从xml文件中读取,无论是在网络上还是本地文件。
use XML::Simple;
use LWP;
use Data::Dumper;
#request path
print "Enter pathn";
my $input = <STDIN>;
my $data;
chomp $input;
print "Path : $inputn";
if ($input =~ /http/)
{
print "This is a webpagen";
my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new( GET => $input );
my $res = $ua->request( $req );
print Dumper (XML::Simple->new()->XMLin( $res->content ));
}
else
{
print "This is a local pathn";
$xml = new XML::Simple;
$data = $xml ->XMLin($input);
print Dumper($data);
}
print "Type in keyword to search: n";
my $inputsearch = <STDIN>;
chomp $inputsearch;
print "You typed --> $inputsearchn";
Dumper($data) =~ m/$inputsearch/;
$after = "$'";
$result = $after =~ /{...}/;
print $result;
好吧,说真的。请不要使用XML::Simple
。即使XML::Simple
说:
不鼓励在新代码中使用该模块。其他模块提供了更直接和一致的接口。
我将猜测XML的外观,并让您了解如何从中提取信息。如果您能提供一个更好的XML示例,我将进行更新。
<root>
<item name="Berry-Berry Belgian Waffles">
<calories>900</calories>
<price>$8.95</price>
<description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
</item>
</root>
你可以这样处理:
#!/usr/bin/env perl
use strict;
use warnings;
use XML::Twig;
my $twig = XML::Twig->new( 'pretty_print' => 'indented' );
$twig->parse( *DATA );
foreach my $item ( $twig -> get_xpath ( '//item' ) ) {
print "Name: ", $item -> att('name'),"n";
foreach my $element ( $item -> children ) {
print $element -> tag,": ", $element -> trimmed_text,"n";
}
}
__DATA__
<root>
<item name="Berry-Berry Belgian Waffles">
<calories>900</calories>
<price>$8.95</price>
<description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
</item>
</root>
对于XML::Twig
,您可以通过att
访问"属性",通过tag
访问元素名称,通过text
或trimmed_text
访问内容。
所以上面会打印:
Name: Berry-Berry Belgian Waffles
calories: 900
price: $8.95
description: Light Belgian waffles covered with an assortment of fresh berries and whipped cream