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
多行匹配PERL_Perl_Multiline_Fileslurp - Fatal编程技术网

多行匹配PERL

多行匹配PERL,perl,multiline,fileslurp,Perl,Multiline,Fileslurp,我有一个简短的问题 我正在尝试匹配一个特定的mulitline实例。问题是,当我执行代码时,它只打印我编辑的内容,而不打印包含它的整个文件 比如说。这是我的意见: JJJ 1234 123.00 1234.28 123456.00 1234567.72 constant ld;afksd;l REst of file blah blah blah...rest of file and other info I neeed etc.

我有一个简短的问题

我正在尝试匹配一个特定的mulitline实例。问题是,当我执行代码时,它只打印我编辑的内容,而不打印包含它的整个文件

比如说。这是我的意见:

JJJ
1234          123.00     1234.28               123456.00     1234567.72 constant
ld;afksd;l REst of file blah blah blah...rest of file and other info I neeed etc.
我的目标是:

JJJ 1234          123.00     1234.28               123456.00     1234567.72 constant
ld;afksd;l REst of file blah blah blah...rest of file and other info I neeed etc.
因此,基本上,我只是尝试使用
JJJ
或任何其他1个或更多大写字母的变体将数据压缩到行中

但是,当我这样做时,我只得到以下信息:

JJJ 1234          123.00     1234.28               123456.00     1234567.72 constant
我只得到那个,只有那个,而不是我需要的文件中的其他信息。我知道有一个简单的解决方案,但我对
perl
非常陌生,不能完全理解它

这是我的代码,也许你们中的一些人会有建议

use File::Slurp;
my $text = read_file( 'posf.txt' );
while ($text =~ /(^[A-Z]+)(\d+.*?\.\d+ Acquired$)/gism) {
$captured = $1." ".$2;
$captured =~ s/\n//gi;

print $captured."\n";
}
任何帮助都会很好。我知道我只是告诉程序打印“已捕获”,但我不知道如何让它打印文件的其余部分,并将行拖到所需的位置


我希望我的问题是有意义的,不难理解,如果我可以进一步询问,请告诉我。

希望我正确理解了您的问题:您希望删除文本中每行后面的换行符,仅包含大写字母。请尝试以下代码:

#!/usr/bin/perl

use strict;
use warnings;

my $text = qq{JJJ
1234          123.00     1234.28               123456.00     1234567.72 constant
ld;afksd;l REst of file blah blah blah...rest of file and other info I neeed etc.
JJJ
1234          123.00     1234.28               123456.00     1234567.72 constant
ld;afksd;l REst of file blah blah blah...rest of file and other info I neeed etc.
};

$text =~ s/(^[A-Z]+) #if the line starts with at least 1 capital letter
      \r?            #followed by optional \r - for DOS files
      \n$/           #followed by \n
      $1 /mg;        #replace it with the 1-st group and a space
print $text;
它打印:

JJJ 1234          123.00     1234.28               123456.00     1234567.72 constant
ld;afksd;l REst of file blah blah blah...rest of file and other info I neeed etc.
JJJ 1234          123.00     1234.28               123456.00     1234567.72 constant
ld;afksd;l REst of file blah blah blah...rest of file and other info I neeed etc.

我没有从文件中读取文本来显示测试数据。但是你可以很容易地添加
read\u file
call。

你能写一个尽可能小的例子来重现你的错误吗?目前,您正在regexp中使用word
Acquired
,而您的数据中缺少该词。很抱歉…让我重新发布代码..我想将其切换为常量..使用File::Slurp;我的$text=read_文件('posf.txt');而($text=~/(^[A-Z]+)(\d++..\d+常量$)/gism){$captured=$1.“$2;$captured=~s/\n//gi;print$captured.\n”}看,怎么了,谢谢..我只需要弄清楚如何使用这个模块而不丢失文件的其余部分…似乎s/起了作用!谢谢