我将以下所有内容存储在$ 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文件中读取它是在Web上还是在本地文件中。
use XML::Simple;
use LWP;
use Data::Dumper;
#request path
print "Enter path\n";
my $input = <STDIN>;
my $data;
chomp $input;
print "Path : $input\n";
if ($input =~ /http/)
{
print "This is a webpage\n";
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 path\n";
$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 --> $inputsearch\n";
Dumper($data) =~ m/$inputsearch/;
$after = "$'";
$result = $after =~ /{...}/;
print $result;
答案 0 :(得分:3)
好的,认真的。请不要使用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