Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.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,我试图在perl脚本中使用正则表达式修改文本文件中的某些内容,但它不会修改任何内容,而是在textpad中使用它修改的相同正则表达式 输入 所需输出: ccpsd(1).xml-ab 3fdfs2sd 6 Feb 2015.xml ccpsd(2).xml-sdfdgd .df . fds 18 Mar 2015.xml 代码 使用严格; 打开(文件“/out.txt”)| |模具“未找到文件”; 打印文件@换行符; 关闭(文件); 您应该使用double>并指定文件名: m

我试图在perl脚本中使用正则表达式修改文本文件中的某些内容,但它不会修改任何内容,而是在textpad中使用它修改的相同正则表达式

输入

所需输出:

    ccpsd(1).xml-ab 3fdfs2sd 6 Feb 2015.xml
    ccpsd(2).xml-sdfdgd .df . fds  18 Mar 2015.xml
代码

使用严格;
打开(文件“/out.txt”)| |模具“未找到文件”;
打印文件@换行符;
关闭(文件);

您应该使用double
>
并指定文件名:

my $outfile = 'out.txt';
open (FILE, ">> $outfile") || die "File not found";

正则表达式很好,正如本文所测试的。

在我看来,您错过了输出文件的驱动器。
我会:

#/usr/bin/perl
严格使用;
使用警告;
我的$in_文件='G:/in.txt';
我的$out_文件='G:/out.txt';
打开我的$in、、$out_文件或死亡“无法打开“$out_文件”:$!”;
while(){
咀嚼;
s/^(+?)\.doc\s*+?:\s*(CC.++?xml)\./$2-$1.xml/i;
打印$out$\“\n”;
}

只需使用Perl一行程序即可

perl -pe's/(^.+?)\.doc\s*.+?[\:]\s*(CC.+?xml)\./\2-\1.xml/ig'
顺便说一句,你读了吗?你确定你理解了吗?因为您逐行读取输入文件,代码中的
s
修饰符没有任何意义。如果您真的想为多行模式应用regexp,那么应该使用

perl -0777 -pe's/(^.+?)\.doc\s*.+?[\:]\s*(CC.+?xml)\./\2-\1.xml/igs'

您在out.txt中有什么内容?它对我有效,只是你必须使用
$1
$2
而不是
\1
\2
。您还必须
使用警告在脚本的开头。
#!/usr/bin/perl
use strict;
use warnings;

my $in_file  = 'G:/in.txt';
my $out_file = 'G:/out.txt';

open my $in, '<', $in_file or die "Unable to open '$in_file': $!";
open my $out, '>', $out_file or die "Unable to open '$out_file': $!";
while(<$in>) {
    chomp;
    s/^(.+?)\.doc\s*.+?:\s*(CC.+?xml)\./$2-$1.xml/i;
    print $out $_,"\n";
}
perl -pe's/(^.+?)\.doc\s*.+?[\:]\s*(CC.+?xml)\./\2-\1.xml/ig'
perl -0777 -pe's/(^.+?)\.doc\s*.+?[\:]\s*(CC.+?xml)\./\2-\1.xml/igs'