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 将多行字符串变量写入文件_Regex_Perl - Fatal编程技术网

Regex 将多行字符串变量写入文件

Regex 将多行字符串变量写入文件,regex,perl,Regex,Perl,我试图在两个标记之间提取文本文件的内容,并将其存储到另一个文件中 我成功地将输入文件转换为一个多行字符串变量,然后使用regexp成功地获得我想要的变量 但是我无法将变量写入文件,我假设这是因为内部有多个\n的字符串类型 我将感谢任何帮助。(这是我的第一个Perl脚本…) 对于测试,我使用index.html文件,但可以是任何文本文件 编辑:已解决,请参见注释中的更正 下面是我的文档代码: # Extract string between two tags use strict; use wa

我试图在两个标记之间提取文本文件的内容,并将其存储到另一个文件中

我成功地将输入文件转换为一个多行字符串变量,然后使用regexp成功地获得我想要的变量

但是我无法将变量写入文件,我假设这是因为内部有多个\n的字符串类型

我将感谢任何帮助。(这是我的第一个Perl脚本…)

对于测试,我使用index.html文件,但可以是任何文本文件

编辑:已解决,请参见注释中的更正

下面是我的文档代码:

# Extract string between two tags

use strict;
use warnings;

my $inputfile = "";
my $outputfile = "";

# Parse Macro Arguments arguments
if(@ARGV < 2)
{
    print "Usage: perl Macro_name.pl <inputfile.HTML> <outfile.HTML>\n";
    exit(1);
}

$inputfile = $ARGV[0];
$outputfile = $ARGV[1];


my $body="";

# Convert input file to multiple line string #
$body = File_to_Var_Multi_Line($inputfile);

# First tag & Second tag match
if ( $body =~ /(.*)<body(.*?)>(.*)<\/body>/s )     
{                                     # error :
    my $body = $3;                    # $body is local here
                                      # correction :
    #Print to check if extract ok     # declare another variable outside if
    print $body, "\n";
}

# Write to file my match multiple line string #
open(my $fh_body, '>:encoding(UTF-8)', $outputfile) 
or die "Could not open file '$outputfile' $!";

print $fh_body "$body\n";

close $fh_body;

# sub #
sub File_to_Var_Multi_Line
{
    if(@_ < 1)
    {
        print "Usage: line=File_to_Var_Multi_Line<file>\n";
        exit(1);
    }

    my $inputfile_2 = "";
    $inputfile_2 = $_[0];

    open(my $fl_in, '<:encoding(UTF-8)', $inputfile_2)
    or die "Could not open file '$inputfile_2' $!";

    my $line = "";
    my $row_2 = "";

    while (my $row_2 = <$fl_in>)
    {
        $line .= $row_2;
    }
    return $line
}
#提取两个标记之间的字符串
严格使用;
使用警告;
我的$inputfile=“”;
我的$outputfile=“”;
#解析宏参数
如果(@ARGV<2)
{
打印“用法:perl Macro_name.pl\n”;
出口(1);
}
$inputfile=$ARGV[0];
$outputfile=$ARGV[1];
我的$body=“”;
#将输入文件转换为多行字符串#
$body=文件到变量行($inputfile);
#第一个标记和第二个标记匹配
如果($body=~/(.*)(.*)/s)
{#错误:
我的$body=$3;#$body在这里是本地的
#更正:
#打印以检查提取是否正常#在if之外声明另一个变量
打印$body,“\n”;
}
#写入文件我的匹配多行字符串#
打开(我的$fh_正文,'>:encoding(UTF-8)',$outputfile)
或“无法打开文件“$outputfile”$!”;
打印$fh_body“$body\n”;
关闭$fh_机构;
#潜艇#
子文件到变量多行
{
如果(@<1)
{
打印“用法:行=文件\u到\u变量\u多行\n”;
出口(1);
}
我的$inputfile_2=“”;
$inputfile_2=$\u[0];
打开(我的$fl_英寸,尽管如此

您可能会发现“范围运算符”对于迭代文件很有用

例如:

while ( <$fl_in> ) {
    if ( m,<BODY>,i .. m,</BODY>,i ) { 
        print;
    }
}
while(){
如果(m,i..m,i){
打印
}
}

如果您在“body”标记内,则该条件为真。(虽然它是面向行的,所以尾部的内容将被“捕获”).

除了说我无法将变量写入文件之外,你并没有真正解释写入输出文件的错误。另外,regex在解析HTML方面也不是很好。你可能需要使用HTML解析器。我修复了它,你的帖子帮助我问出了什么问题……我在“if”中局部声明了一个新的$body而其中一个存在于外部…所以我的打印是打印“如果”外部的,而不是“如果”内部的…我使用了一个HTML文件进行练习,但我真正需要解析的文件不是HTML。我需要一个独立的方法。我将看一看range操作符,它可能比regexp.Thnks更好。
while ( <$fl_in> ) {
    if ( m,<BODY>,i .. m,</BODY>,i ) { 
        print;
    }
}