我有一堆文本,我试图匹配一组模式,我使用的正则表达式能够匹配模式,但问题是它只匹配第二组,而不是第一组。
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";
}打印
0x1b201
lpfc_740
测试公司
上面的代码可以工作,但它只显示来自第二个组的文本,而不是第一个组。我在这里错过了什么?有更好的方法吗?
我以为它会印出来
0x1a101
lpfc_740
测试公司
0x1b201
lpfc_740
测试公司
\==+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............................. 发布于 2012-03-14 20:22:36
问题是,第一个.*在不阻止匹配的情况下贪婪地进行匹配;因此,它吞噬一切直到第二个World Wide Port Number。您需要将其更改为.*?,就像您已经在模式的其他地方使用一样。(.*的其他实例也是如此。)
发布于 2012-03-14 21:03:30
应该是这样的
$text =~ m/FibreChannel SCSI Interface.*?World Wide Port Number\.*([a-z0-9]+).*?Driver\.+(\w+).*?Vendor Name\.+([a-zA-Z ]+).*?\n/sgmphttps://stackoverflow.com/questions/9709608
复制相似问题