Perl正则表达式匹配多个多线组

Sum*_*edh 2 regex perl

我有一堆文本,我试图匹配一组模式,我正在使用的正则表达式能够匹配模式,但问题是它只匹配第二组而不是第一组.

open(FILE, "match.txt") || die("Could not open file ");
my $text = do { local $/; <FILE> };

while( $text =~ m/FibreChannel SCSI Interface.*World Wide Port Number\.*(.*?)\n.*Driver\.+(.*?)\n.*Vendor Name\.+(.*?)\n/sgmp )
{
    print "$1\n$2\n$3\n";
}
Run Code Online (Sandbox Code Playgroud)

打印

0x1b201
lpfc_740
测试公司

上面的代码有效,但它只显示第二组中的文本而不是第一组.我在这里错过了什么?有一个更好的方法吗?

我以为会打印出来

0x1a101
lpfc_740
测试公司
0x1b201
lpfc_740
测试公司

------------------------ match.txt包含

\==+FibreChannel SCSI Interface : 
        |----Link State.........................................Down
        |----World Wide Port Number.............................0x1a101
        \==+SCSI Interface : 
           |----Driver..........................................lpfc_740
           |----Queue Depth.....................................2038 
           \==+PCI Device : 
              |----Bus..........................................0x06 
              |----Vendor Name..................................Test Corporation
              |----Slot Description.............................

\==+FibreChannel SCSI Interface : 
        |----Link State.........................................Down
        |----World Wide Port Number.............................0x1b201
        \==+SCSI Interface : 
           |----Driver..........................................lpfc_740
           |----Queue Depth.....................................2038 
           \==+PCI Device : 
              |----Bus..........................................0x0a 
              |----Vendor Name..................................Test Corporation
              |----Slot Description.............................          
Run Code Online (Sandbox Code Playgroud)

rua*_*akh 5

问题是,第一个.*是尽可能地贪婪地匹配而不阻止匹配; 所以,直到第二次吞下一切World Wide Port Number.您需要将其更改为.*?,就像您已经在模式中的其他位置使用一样.(并且,类似于其他实例.*.)