【发布时间】:2019-12-10 02:29:14
【问题描述】:
我有一个如下所示的文件:
string 1 {
abc { session 1 }
fairPrice {
ID LU0432618274456
Source 4
service xyz
}
}
string 2 {
abc { session 23 }
fairPrice {
ID LU036524565456171
Source 4
service tzu
}
}
我的程序应该使用给定的搜索参数(例如“字符串 1”)读取文件并搜索完整的块,直到“}”并从文件中删除该部分。有人可以帮忙吗...到目前为止我有一些代码,但是我怎样才能删除并再次保存到同一个文件中?
my $fh = IO::File->new( "$fname", "r" ) or die ( "ERROR: Strategy file \"$fname\" not found." );
while($line=<$fh>)
{
if ($line =~ /^\s*string 1\s*\w+\s*\{\s*$/) {
$inside_json_msg = 1;
$msg_json .= $line;
}
else {
if ($inside_json_msg)
{
if ($line =~ m/^\}\s*$/) {
$msg_json.= $line if defined($line);
$inside_json_msg = 0;
} else {
$msg_json .= $line;
}
}
}
}
【问题讨论】:
-
我会推荐使用Regexp::Grammars
-
等等,你的代码提到了 JSON。数据实际上是 JSON 吗?如果是这样,您可以使用一些库。