perl 提取 2 个分隔符之间的子字符串

perl extract substrings between 2 delimiters

我有一个类似于下面的文件。

Jobs                             |Status |Wait Reason |Wait Local |Wait Remote |Running |Passing |Failing |Skipped |Failures 
--------------------------------- ------- ------------ ----------- ------------ -------- -------- -------- -------- ---------
build1                           |FAILED |            |           |            |        |      8 |     5  |        |         
build2                `          |PASSED |            |           |            |        |      2 |        |        |         
build3                           |PASSED |            |           |            |        |      6 |        |        |        

我需要提取分隔符 '|' 之间的子字符串。我尝试使用下面的正则表达式

$row=~ s/ //g; (@substrings)= $row =~ /|(.*?)\|/g;

结果是

0 :
1 : build1
2 :
3 : FAILED
4 :
5 :
6 :
7 :
8 :
..

我也使用了拆分函数 split '|',$row; 但它返回的数据是

b
u
i
l
d
1


|
F
..

我正在尝试提取以下数据。

$substrings[0]=build1
$substrings[1]=FAILED
$substrings[2]=(null) 

等等。

如何提取子字符串?

考虑使用拆分功能。下面是分隔符为 space 的示例,您可以将其替换为 '|'管道符号。

my $str = "ab cd ef gh ij";
my @words = split / /, $str;

正如 Maddy 和 sebnukem 提到的,分裂就是答案。为此仅使用正则表达式是可行的,但由于行首和行尾缺少管道 (|),因此更加复杂。这是一个读取您的数据文件的脚本:

#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

foreach my $line (<>) {
#   print $line;
    chomp($line);
    next unless $line =~ /\|/; # only try lines with pipes in them
    my @fields = split(/\s+\|/,$line);
    print Dumper(\@fields);
}

这里有一个例子运行它:

chicks$ cat data.txt | ./proc_data.pl 
$VAR1 = [
          'Jobs',
          'Status',
          'Wait Reason',
          'Wait Local',
          'Wait Remote',
          'Running',
          'Passing',
          'Failing',
          'Skipped',
          'Failures '
        ];
$VAR1 = [
          'build1',
          'FAILED',
          '',
          '',
          '',
          '',
          '      8',
          '     5',
          '',
          '         '
        ];
$VAR1 = [
          'build2                `',
          'PASSED',
          '',
          '',
          '',
          '',
          '      2',
          '',
          '',
          '         '
        ];
$VAR1 = [
          'build3',
          'PASSED',
          '',
          '',
          '',
          '',
          '      6',
          '',
          '',
          '        '
        ];

请注意,split 将正则表达式放在斜杠中,而不是引号中,反斜杠必须转义:\|。我还在结果的右侧包含了一个 \s+ 到 trim 的空格。正则表达式另一侧的 \s+ 将获得左侧空格,如 ' 6' 中那样。如果您希望它匹配 0 个或更多空格,您应该在这些地方使用 * 而不是 +