Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 在Perl中提取{}之间的字符串_Regex_Perl - Fatal编程技术网

Regex 在Perl中提取{}之间的字符串

Regex 在Perl中提取{}之间的字符串,regex,perl,Regex,Perl,我将以下所有内容存储在$data中 'Berry-Berry Belgian Waffles' => { 'calories' => '900', 'price' => '$8.95',

我将以下所有内容存储在$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;
使用LWP;
使用数据::转储程序;
#请求路径
打印“输入路径\n”;
我的$input=;
我的$数据;
chomp$输入;
打印“路径:$input\n”;
如果($input=~/http/)
{
打印“这是一个网页\n”;
my$ua=LWP::UserAgent->new;
my$req=HTTP::Request->new(GET=>$input);
我的$res=$ua->请求($req);
打印转储程序(XML::Simple->new()->XMLin($res->content));
}
其他的
{
打印“这是一个本地路径\n”;
$xml=newxml::Simple;
$data=$xml->XMLin($input);
打印转储程序(数据);
}
打印“键入要搜索的关键字:\n”;
我的$inputsearch=;
chomp$inputsearch;
打印“您键入的-->$inputsearch\n”;
转储程序($data)=~m/$inputsearch/;
“$”之后的$;
$result=$after=~/{…}/;
打印$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>

您没有字符串。你有一个数据结构。你试图做的事情的实际目标是什么?你在用一种奇怪的方式弯曲东西。您将XML数据转换为perl数据结构,然后对其进行字符串化以查找内容。期望的输出究竟是什么?你为什么需要这个。请提供更多关于您的总体目标的信息,以便我们可以帮助您了解解决任务实际需要做些什么,而不是通过非最佳方法解决您创建的问题。另外,
请使用strict
请使用warnings
。很抱歉,没有在那里看到xml。虽然您可以使用此
/\{(.+?)\}/s
。但不要使用
xml::Simple
。请使用一些示例XML,我们将向您展示如何正确解析它。XML::LibXML和XML::Twig是很好的选择。
#!/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>
Name: Berry-Berry Belgian Waffles
calories: 900
price: $8.95
description: Light Belgian waffles covered with an assortment of fresh berries and whipped cream