Perl Regex New Line and no Chartial

发布于 2025-02-05 02:34:55 字数 518 浏览 2 评论 0原文

我正在尝试从输入文件中提取多行,以输出文件线仅使用Perl中的Regex包含头。逻辑是在数组中添加令牌,然后将traverse Array添加为头部。在添加阵列中的令牌时,添加了匹配的正则匹配的正则表达式新线条,没有字符。

my @arr = split("\n",$str);

foreach my $token (@arr) {
    print "Inside for\n";
    if($token =~ m[head])
    {
        print "Inside if";
        print $token;
    }
} 
**File Content**
**InputFile.txt**

- text1
- text2
- head

- text4
- text5
- non head

- text8
- text9
- head

**OutputFile.txt**
- text1
- text2
- head

- text8
- text9
- head

I am trying extract multiple line from an Input file to Output file lines only containing head using regex in perl. Logic is to add lines as token in an array and then traverse array for head. Got stuck matching regex pattern new line and no character while adding lines as token in array.

my @arr = split("\n",$str);

foreach my $token (@arr) {
    print "Inside for\n";
    if($token =~ m[head])
    {
        print "Inside if";
        print $token;
    }
} 
**File Content**
**InputFile.txt**

- text1
- text2
- head

- text4
- text5
- non head

- text8
- text9
- head

**OutputFile.txt**
- text1
- text2
- head

- text8
- text9
- head

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妞丶爷亲个 2025-02-12 02:34:55

可以在段落中读取输入( -00 switch> -00 switch )实际上总是像那样的空白,如果它具有(在这种情况下为结束),则打印一个段落,

perl -00 -wne'print if /\n\s*- head\s*\z/'  file

我使用\ z 断言但是$在给定示例中也很好。


在脚本中,这是通过设置输入记录分隔仪

use warnings;
use strict;

local $/ = "\n\n";

while (<>) { 
    print if /\n\s*- head\s*\z/;
}

我们想要 local ,请不要为整个运行中的所有内容更改$/(在较大的程序中)。


评论将问题扩大了一点,以便

...头后有2-3行,也应包括在内

在段落中读取文件时容易容纳的行 - 只需更改正则义务,以便在任何地方都匹配模式/线(不仅在结尾),这样的话,

/\n\s*- head\s*\n/

如果线处于末尾,仍然可以匹配。

Can read input in paragraphs (-00 switch), if there is really always a blank like there, and print a paragraph if it has (ends with in this case) a desired pattern

perl -00 -wne'print if /\n\s*- head\s*\z/'  file

I use the \z assertion but $ is fine as well in the given example.


In a script this is done by setting the input record separator

use warnings;
use strict;

local $/ = "\n\n";

while (<>) { 
    print if /\n\s*- head\s*\z/;
}

We want local so to not change $/ for everything in the whole run (in a larger program).


A comment expands the question a little so that

... what of there are 2-3 lines after head and that should also be included

This is simple to accommodate when a file is read in paragraphs -- just change the regex so that it matches a pattern/line anywhere (not only at the end), like

/\n\s*- head\s*\n/

This still matches if the line is at the end, as well.

萧瑟寒风 2025-02-12 02:34:55

逐行阅读文件时,可以使用“滑动窗口”技术实现预期的输出。

#!/usr/bin/perl
use warnings;
use strict;

my @buffer;
while (<>) {
    if (/- head$/) {
        print splice @buffer;
        print;
    } elsif (/^$/) {  # Same as ("\n" eq $_)
        @buffer = ("\n");
    } else {
        push @buffer, $_;
    }
}

The expected output can be achieved using the "sliding window" technique when reading the file line by line.

#!/usr/bin/perl
use warnings;
use strict;

my @buffer;
while (<>) {
    if (/- head$/) {
        print splice @buffer;
        print;
    } elsif (/^$/) {  # Same as ("\n" eq $_)
        @buffer = ("\n");
    } else {
        push @buffer, $_;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文