在Perl中使用多个反向引用

时间:2019-05-05 21:50:57

标签: regex perl nsregularexpression backreference

我试图在Perl中使用多个反向引用来匹配5种不同的模式,但是除了第一个之外,我都没有匹配。

我尝试了以下操作:

my $string = ">abc|XYUXYU|KIOKEIK_7XNCU Happy, not-happy apple banana X ORIG=Came from trees NBMR 12345 OZ=1213379 NG=popZ AZ=2 BU=1";
$string =~ m/>(abc)|(.*)|.*ORIG=(.*)[A-Z].*NG=(.*)\s(.*)\s/;

print "First match should be 'abc'. We got: $1\n";
print "Second match should be 'XYUXYU'. We got: $2\n";
print "Third match should be 'Came from trees'. We got: $3\n";
print "Fourth match should be 'popZ'. We got: $4\n";
print "Fifth match should be 'AZ=2'. We got: $5\n";

我想要作为输出:

First match should be 'abc'. We got: abc
Second match should be 'XYUXYU'. We got: XYUXYU
Third match should be 'Came from trees'. We got: Came from trees
Fourth match should be 'popZ'. We got: popZ
Fifth match should be 'AZ=2'. We got: AZ=2

任何线索如何在Perl上以正确的方式解决此问题?

1 个答案:

答案 0 :(得分:3)

您必须通过在|之前加上\来逃避a|b,否则它们意味着交替(a匹配b*)。对于第三场比赛,您必须通过添加?来使量词#!/usr/bin/perl use strict; use warnings; my $string = ">abc|XYUXYU|KIOKEIK_7XNCU Happy, not-happy apple banana X ORIG=Came from trees NBMR 12345 OZ=1213379 NG=popZ AZ=2 BU=1"; $string =~ m/>(abc)\|(.*)\|.*ORIG=(.*?)\s[A-Z]+.*NG=(.*)\s(.*)\s/; print "First match should be 'abc'. We got: $1\n"; print "Second match should be 'XYUXYU'. We got: $2\n"; print "Third match should be 'Came from trees'. We got: $3\n"; print "Fourth match should be 'popZ'. We got: $4\n"; print "Fifth match should be 'AZ=2'. We got: $5\n"; 不贪婪。并且您需要稍微调整第三个捕获组之后的模式,以使空间与至少一个上位字符匹配(此处尚不完全清楚总体可能性是什么,因为您只是给出了一个示例而没有进一步的细节。可能需要进一步调整) )

First match should be 'abc'. We got: abc
Second match should be 'XYUXYU'. We got: XYUXYU
Third match should be 'Came from trees'. We got: Came from trees
Fourth match should be 'popZ'. We got: popZ
Fifth match should be 'AZ=2'. We got: AZ=2

输出:

$