perl 在特定模式后提取字符串

perl extract string after specific pattern

我想提取(使用 perl)xxx(块之后的字符串:)和 prod(里程碑之后的字符串:)。字符串(在 Block: 和 Milestone: 之后)和空格数不是标准的。我只能使用底部命令 grep 整行:

use strict;
use warnings;

my $file = 'xxx.txt';
open my $fh, '<', $file or die "Could not open '$file' $!\n";
while (my $line = <$fh>){
    chomp $line;
#   my @stage_status = $line =~ /(\:.*)\s*$/;
my @stage_status = $line =~ /\b(Block)(\W+)(\w+)/;
    foreach my $stage_statuss (@stage_status){
        print "$stage_statuss\n";
    }
    }

文件中的行示例:

| Block:                   | xxx | Milestone:           | prod        |

你可以用一个简单的 awk 来做到这一点。通过设置适当的字段分隔符值,我们可以获得所需的值。只需将字段分隔符设置为管道,后跟 space 或 space 出现,然后在主程序中检查条件,如果第二个字段是块:然后打印第四个字段。

awk -F'\|[[:space:]]+|[[:space:]]+' '=="Block:"{print } =="Milestone:"{print }' Input_file


第二个解决方案: 与我上面的第一个解决方案几乎相同的解决方案,唯一的问题是这里只为 [=12 创建 1 个字段分隔符=].

awk -F'([[:space:]]+)?\|([[:space:]]+|$)' '=="Block:"{print } =="Milestone:"{print }' Input_file

使用gnu grep你可以做到:

grep -oP '\b(Block|Milestone)\W+\K\w+' file

xxx
prod

RexEx 详情:

  • \b;单词边界
  • (Block|Milestone):匹配BlackMilestone
  • \W+:匹配1+个非单词字符
  • \K: 重置匹配信息
  • \w+:匹配1+个单词字符

更新:

根据 OP 编辑​​的问题建议的 perl 代码:

use strict;
use warnings;

my $file = 'xxx.txt';
open my $fh, '<', $file or die "Could not open '$file' $!\n";

while (my $line = <$fh>){
    chomp $line;
    print "checking: $line\n";
    my @stage_status = $line =~ /\b(?:Block|Milestone)\W+(\w+)/g;
    
    foreach my $stage_statuss (@stage_status){
       print "$stage_statuss\n";
    }
}

输出:

checking: | Block:                   | xxx | Milestone:           | prod        |
xxx
prod