![](/img/trans.png)
[英]perl find lines containing string 1 then extract character between string 2 and 3
[英]Extract String between { } in Perl
我將以下所有內容存儲在$ 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;
好,認真 請不要使用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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.